-
Notifications
You must be signed in to change notification settings - Fork 0
/
tsaurus_bot.py
68 lines (47 loc) · 2.37 KB
/
tsaurus_bot.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import logging
from telegram import ParseMode, InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import Updater, InlineQueryHandler, CommandHandler
import util.bot_tools as bt
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO,
filename="main.log")
logger = logging.getLogger(__name__)
INFO_MESSAGES = {'start': 'Hi!\nThis bot currently supports only inline mode. Try it out in any chat!',
'help': "Use this bot via inline mode. Type\n@TsaurusBot _word or phrase_\nin any chat"}
def start(update, context):
"""Send a message when the command /start is issued."""
mw_logo = "https://dictionaryapi.com/images/info/branding-guidelines/MWLogo_LightBG_120x120_2x.png"
kb_markup = InlineKeyboardMarkup.from_button(InlineKeyboardButton('Try in this chat', switch_inline_query_current_chat=''))
update.message.reply_photo(mw_logo, caption=INFO_MESSAGES['start'], reply_markup=kb_markup)
def show_help(update, context):
"""Send a message when the command /help is issued."""
update.message.reply_text(INFO_MESSAGES['help'], parse_mode=ParseMode.MARKDOWN)
def inlinequery(update, context):
"""Handle the inline query."""
user = update.inline_query.from_user['username']
query = ''.join(c for c in update.inline_query.query if c.isalnum() or c is ' ')
if query.strip() != '':
logger.info(f"User @{user} searched \"{query}\"")
query_results = bt.ask_mw_thesaurus(query)
update.inline_query.answer(query_results)
def error(update, context):
"""Log Errors caused by Updates."""
logger.warning(f"Update {update} caused error {context.error}")
def main():
# Create the Updater and pass it your bot's token.
# Make sure to set use_context=True to use the new context based callbacks
# Post version 12 this will no longer be necessary
from config import BOT_TOKEN
updater = Updater(BOT_TOKEN, use_context=True)
dp = updater.dispatcher
dp.add_handler(CommandHandler("start", start))
dp.add_handler(CommandHandler("help", show_help))
dp.add_handler(InlineQueryHandler(inlinequery))
dp.add_error_handler(error)
updater.start_polling()
logger.info("BOT DEPLOYED. Ctrl+C to terminate")
updater.idle()
if __name__ == '__main__':
main()