-
Notifications
You must be signed in to change notification settings - Fork 0
/
imagic8ball.py
149 lines (112 loc) · 4.43 KB
/
imagic8ball.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env python
# -*- coding: utf8 -*-
import telebot
import sqlite3
from random import randint
from settings import token, texts
# Connect (or create) DB sqlight3 for save user's language
try:
sqlite_connection = sqlite3.connect('db.sqlite3')
cursor = sqlite_connection.cursor()
sqlite_create_table_query = "CREATE TABLE user_language (id INTEGER PRIMARY KEY, language TEXT NOT NULL)"
cursor.execute(sqlite_create_table_query)
sqlite_connection.commit()
cursor.close()
except sqlite3.Error as error:
print('Error : ', error)
finally:
if (sqlite_connection):
sqlite_connection.close()
# Connect telegram
bot = telebot.TeleBot(token)
# Create menu with commands
bot.set_my_commands([
telebot.types.BotCommand('/en', 'English'),
telebot.types.BotCommand('/fr', 'Français (French)'),
telebot.types.BotCommand('/es', 'Español (Spanish)'),
telebot.types.BotCommand('/ru', 'Русский (Russian)')
])
def get_user_language(user_id,tg_lang):
# Connect (or create) DB sqlight3 for save user's language
try:
sqlite_connection = sqlite3.connect('db.sqlite3')
except sqlite3.Error as error:
print('Error : ', error)
# Get user language from DB
cursor = sqlite_connection.cursor()
cursor.execute("SELECT language FROM user_language WHERE id = ? LIMIT 0, 1", (user_id,))
record = cursor.fetchone()
cursor.close()
if record != None:
# Get user language from DB
language = record[0]
else:
# Get language of telegram
language = 'en'
if str(tg_lang) in ['en','fr','es','ru']:
language = str(tg_lang)
# Add user to DB
cursor = sqlite_connection.cursor()
cursor.execute("INSERT INTO user_language (id, language) VALUES (?, ?)", (user_id, language))
sqlite_connection.commit()
cursor.close()
if sqlite_connection:
sqlite_connection.close()
return language
def update_user_language(user_id,language):
# Connect (or create) DB sqlight3 for save user's language
try:
sqlite_connection = sqlite3.connect('db.sqlite3')
except sqlite3.Error as error:
print('Error : ', error)
# Update user language in DB
cursor = sqlite_connection.cursor()
cursor.execute("UPDATE user_language SET language = ? WHERE id = ?", (language, user_id))
sqlite_connection.commit()
cursor.close()
if sqlite_connection:
sqlite_connection.close()
return language
# Commands
@bot.message_handler(commands=['start','en','fr','es','ru'])
def get_commands(command):
# Get user language
if command.text == '/start':
language = get_user_language(str(command.from_user.id),str(command.from_user.language_code))
elif command.text == '/en':
language = update_user_language(str(command.from_user.id),'en')
elif command.text == '/fr':
language = update_user_language(str(command.from_user.id),'fr')
elif command.text == '/es':
language = update_user_language(str(command.from_user.id),'es')
elif command.text == '/ru':
language = update_user_language(str(command.from_user.id),'ru')
# Send first message
if command.text in ['/start','/en','/fr','/es','/ru']:
# Upload sticker
sticker = open('img/question.png', 'rb')
# First message from bot
bot.send_sticker(command.from_user.id, sticker)
bot.send_message(command.from_user.id, text=texts[language][1])
else:
# Unrecognized command
bot.send_message(command.from_user.id, text=texts[language][3])
@bot.callback_query_handler(func=lambda call: True)
@bot.message_handler(content_types=['text'])
def answer(message):
# Get user language
language = get_user_language(str(message.from_user.id),str(message.from_user.language_code))
# Create button
keyboard = telebot.types.InlineKeyboardMarkup()
button = telebot.types.InlineKeyboardButton(texts[language][2], callback_data='2')
keyboard.add(button)
# Ubload sticker
photo_file = 'img/' + language + '/' + str(randint(1,20)) + '.png'
sticker = open(photo_file, 'rb')
bot.send_sticker(message.from_user.id, sticker, reply_markup=keyboard)
while True:
try:
# Listen commmands from users
bot.infinity_polling(interval=0)
except Exception:
pass