-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
63 lines (44 loc) · 1.36 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
import shutil
import requests
import json
from boto3.resources.model import Request
import telegram
import os
import logging
from bot import plain_handler
OK_RESPONSE = {
'statusCode': 200,
'headers': {'Content-Type': 'application/json'},
'body': json.dumps({'statusCode': 200})
}
ERROR_RESPONSE = {
'statusCode': 400,
'body': json.dumps({'statusCode': 400})
}
logger = logging.getLogger()
if logger.handlers:
for handle in logger.handlers:
logger.removeHandler(handle)
logging.basicConfig(level=logging.INFO)
def configure_telegram():
TELEGRAM_TOKEN = os.environ.get('TELEGRAM_TOKEN')
if not TELEGRAM_TOKEN:
logger.error('environment variable `TELEGRAM_TOKEN` must be set')
raise NotImplementedError
return telegram.Bot(TELEGRAM_TOKEN)
def handler(event, context):
bot = configure_telegram()
logger.info('event: {}'.format(event))
if event.get('httpMethod') == 'POST' and event.get('body'):
logger.info('message received')
update = telegram.Update.de_json(json.loads(event.get('body')), bot)
plain_handler(bot, update, logger)
return OK_RESPONSE
return ERROR_RESPONSE
def set_webhook(url):
logger.info('url: {}'.format(url))
bot = configure_telegram()
webhook = bot.set_webhook(url)
if webhook:
return OK_RESPONSE
return ERROR_RESPONSE