-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
98 lines (79 loc) · 3 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
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
import os
import sys
import json
import logging
import telegram
from database import Database
from commands import command_handler, default_handler, is_command, parse_command
from exceptions import CommandNotFound, CharacterNotFound, CampaignNotFound, InvalidCommand, NotADM
from telegram.ext import Updater
from telegram.ext import CommandHandler
logger = logging.getLogger()
if logger.handlers:
for handler in logger.handlers:
logger.removeHandler(handler)
logging.basicConfig(format='%(message)s', level=logging.INFO)
OK_RESPONSE = {
'statusCode': 200,
'headers': {'Content-Type': 'application/json'},
'body': json.dumps('ok')
}
ERROR_RESPONSE = {
'statusCode': 500,
'body': json.dumps('Oops, something went wrong!')
}
def configure_telegram():
"""
Configures the bot with a Telegram Token.
Returns a bot instance.
"""
TELEGRAM_TOKEN = os.environ.get('TELEGRAM_TOKEN')
if not TELEGRAM_TOKEN:
logger.error('The TELEGRAM_TOKEN must be set')
raise NotImplementedError
return telegram.Bot(TELEGRAM_TOKEN)
def webhook(event, context):
"""
Runs the Telegram webhook.
"""
bot = configure_telegram()
logger.info(json.loads(event.get('body')))
if event.get('httpMethod') == 'POST' and event.get('body'):
update = telegram.Update.de_json(json.loads(event.get('body')), bot)
if not is_command(update):
return OK_RESPONSE
db = Database()
chat_id = update.message.chat.id
username = update.message.from_user.username if update.message.from_user.username else update.message.from_user.first_name
command = parse_command(update.message.text)
txt_args = ' '.join(update.message.text.split(' ')[1:])
try:
command_handler(command)(bot, update, command, txt_args, username, chat_id, db)
except (CommandNotFound, InvalidCommand):
default_handler(bot, update, 'Invalid command or command not supported')
except CharacterNotFound:
default_handler(bot, update, 'Character not found. Cannot execute command')
except CampaignNotFound:
default_handler(bot, update, 'Campaign not found. There must be an active campaign')
except json.JSONDecodeError:
default_handler(bot, update, 'Error parsing JSON')
except NotADM:
default_handler(bot, update, f'Only the Dungeon Master can execute {command} command')
#except Exception:
# logger.error(sys.exc_info()[2])
# default_handler(bot, update, 'Unhandled error. Check server logs for more details')
return OK_RESPONSE
def set_webhook(event, context):
"""
Sets the Telegram bot webhook.
"""
logger.info('Event: {}'.format(event))
bot = configure_telegram()
url = 'https://{}/{}/'.format(
event.get('headers').get('Host'),
event.get('requestContext').get('stage'),
)
webhook = bot.set_webhook(url)
if webhook:
return OK_RESPONSE
return ERROR_RESPONSE