-
Notifications
You must be signed in to change notification settings - Fork 0
/
lambda_function.py
67 lines (54 loc) · 1.74 KB
/
lambda_function.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
import json
import os
import requests
BOT_TOKEN = os.environ.get('TOKEN')
BOT_CHAT_ID = os.environ.get('CHATID')
BASE_URL = 'https://api.telegram.org/bot' + BOT_TOKEN
# ============================================================
# TELEGRAM API
# ============================================================
def sendMessage(text, mode=""):
requests.post(BASE_URL + '/sendMessage', data={
"chat_id": BOT_CHAT_ID,
"text": text,
})
def sendPhoto(photo):
requests.post(BASE_URL + '/sendPhoto', data={
"chat_id": BOT_CHAT_ID,
"photo": photo,
})
# ============================================================
# COMMAND HANDLERS
# ============================================================
def handle_command1(args):
sendMessage(args)
def handle_command2(args):
sendMessage(args)
def handle_command3(args):
sendMessage(args)
# ============================================================
# MAIN
# ============================================================
def lambda_handler(event, context):
try:
messageContent = json.loads(event["body"])["message"]["text"]
commandMap = {
"/command1": lambda x: handle_command1(x),
"/command2": lambda x: handle_command2(x),
"/command3": lambda x: handle_command3(x),
}
tokens = messageContent.split(' ')
command = tokens[0]
args = []
if len(tokens) > 1:
args = tokens[1:]
if command in commandMap.keys():
commandMap[command](args)
else:
sendMessage(f'Command {messageContent} not found!')
except Exception as ex:
sendMessage(str(ex))
return {
'statusCode': 200,
'body': json.dumps('OK')
}