- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_function.py
More file actions
Latest commit
67 lines (54 loc) · 1.74 KB
/
Copy pathlambda_function.py
File metadata and controls
67 lines (54 loc) · 1.74 KB
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
importjson
importos
importrequests
BOT_TOKEN=os.environ.get('TOKEN')
BOT_CHAT_ID=os.environ.get('CHATID')
BASE_URL='https://api.telegram.org/bot'+BOT_TOKEN
# ============================================================
# TELEGRAM API
# ============================================================
defsendMessage(text, mode=""):
requests.post(BASE_URL+'/sendMessage', data={
"chat_id": BOT_CHAT_ID,
"text": text,
})
defsendPhoto(photo):
requests.post(BASE_URL+'/sendPhoto', data={
"chat_id": BOT_CHAT_ID,
"photo": photo,
})
# ============================================================
# COMMAND HANDLERS
# ============================================================
defhandle_command1(args):
sendMessage(args)
defhandle_command2(args):
sendMessage(args)
defhandle_command3(args):
sendMessage(args)
# ============================================================
# MAIN
# ============================================================
deflambda_handler(event, context):
try:
messageContent=json.loads(event["body"])["message"]["text"]
commandMap= {
"/command1": lambdax: handle_command1(x),
"/command2": lambdax: handle_command2(x),
"/command3": lambdax: handle_command3(x),
}
tokens=messageContent.split(' ')
command=tokens[0]
args= []
iflen(tokens) >1:
args=tokens[1:]
ifcommandincommandMap.keys():
commandMap[command](args)
else:
sendMessage(f'Command {messageContent} not found!')
exceptExceptionasex:
sendMessage(str(ex))
return {
'statusCode': 200,
'body': json.dumps('OK')
}