-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
76 lines (48 loc) · 1.77 KB
/
server.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
import logging
from conversation import Conversation
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
import os
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
logger = logging.getLogger(__name__)
HANDLERS = {}
def get_handler(uid):
if not HANDLERS.get(uid, None):
HANDLERS[uid] = Conversation()
return HANDLERS[uid]
def start(update, context):
"""Send a message when the command /start is issued."""
update.message.reply_text('Hi!')
def help(update, context):
"""Send a message when the command /help is issued."""
update.message.reply_text('Help!')
def on_message(update, context):
"""Echo the user message."""
uid = update.effective_user.id
handler = get_handler(uid)
reply = handler.response(update.message.text)
update.message.reply_text(reply)
def error(update, context):
"""Log Errors caused by Updates."""
logger.warning('Update "%s" caused error "%s"', update, context.error)
def main():
API_KEY = os.environ.get('API_TELEGRAM', None)
if not API_KEY:
print("Please define API_KEY in the environment")
exit(0)
"""Start the bot."""
updater = Updater(API_KEY, use_context=True)
# Get the dispatcher to register handlers
dp = updater.dispatcher
# on different commands - answer in Telegram
dp.add_handler(CommandHandler("start", start))
dp.add_handler(CommandHandler("help", help))
# on noncommand i.e message - echo the message on Telegram
dp.add_handler(MessageHandler(Filters.text, on_message))
# log all errors
dp.add_error_handler(error)
# Start the Bot
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()