-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
40 lines (31 loc) · 1.19 KB
/
main.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
import telebot
from utils import *
from telebot import types
bot = telebot.TeleBot(os.environ.get('token'))
pattern = 'Word: {1}\nAccent: {0}'
@bot.inline_handler(func=lambda query: len(query.query) > 0)
def handle(query):
args = query.query.split()
if len(args) == 2 and args[0] in accents:
bot.answer_inline_query(query.id, form_response(*args), cache_time=0)
track_action(query.from_user.id, query.query)
def form_response(accent, word):
response = []
for i, j in enumerate(search(word), 1):
url = get_audio(j, accent)
if url:
inline_response = types.InlineQueryResultVoice(
voice_url=url['audio'], title=j, id=i,
reply_markup=get_keyboard(url),
caption=pattern.format(accents[accent]['name'], j)
)
response.append(inline_response)
return response
def get_keyboard(url):
examples = url['examples']
markup = types.InlineKeyboardMarkup()
if examples:
markup.add(types.InlineKeyboardButton(text='Examples', url=examples))
return markup
if __name__ == '__main__':
bot.polling(none_stop=True, timeout=100000)