-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandler.py
138 lines (123 loc) · 5.95 KB
/
handler.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import json
import logging
import os
import random
import time
from emoji import emojize
from telegram import Bot, Update
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
RESPONSE = {
"OK": {
'statusCode': 200,
'headers': {'Content-type': 'application/json'},
'body': json.dumps("Ok")
},
"ERROR": {
'statusCode': 400,
'body': json.dumps("Something went wrong")
}
}
def configure_telegram():
TELEGRAM_TOKEN = os.environ.get('TELEGRAM_TOKEN')
if not TELEGRAM_TOKEN:
logging.error(
'TELEGRAM_TOKEN Not found, it must be set before moving forward.')
raise NotImplementedError
return Bot(TELEGRAM_TOKEN)
def populate_excuse():
global excuseDict
excuseDict = {}
officeExcuse = (
"I missed the office because ozone fluttered my car tire in the air.", "The pressure cooker in my house exploded and scared the maid so I had to stay at home", "I was blocked by the tax department of the housing committee who raided my house.", "Loki Chan was again in my lunch box, and I was very upset to go on time")
birthdayExcuse = (
"I remember if I did not eat canteen for lunch!",
"The weather has made me do this. Seriously. In May, to cry loudly in Mumbai! Are not you born in October?", "what!!! I was doing a favor to you by mistake, and was trying to teach you the art of forgiveness.", "I was kidnapped by the aliens and they wiped me from my memory one day ... So, I will remember tomorrow.")
anniversaryExcuse = (
"I thought our wedding anniversary was the day we met, on that day we did not make it official.", "I wanted to surprise you - would not you be surprised if I really remembered it, right? Understandable", "I.P.L. Tickets were sold, so I decided that we can celebrate next week.", "I remembered, but I forgot on the way I passed through the Saki Naka, remember?")
excuseDict['missed office'] = officeExcuse
excuseDict['forgot birthday'] = birthdayExcuse
excuseDict['forgot anniversary'] = anniversaryExcuse
def webhook(event, context):
bot = configure_telegram()
logging.info('Event: {}'.format(event))
populate_excuse()
if event.get('httpMethod') == 'POST' and event.get('body'):
logging.info("Message successfully received")
update = Update.de_json(json.loads(event.get('body')), bot)
chat_id = update.message.chat_id
text = update.message.text
if text == '/start':
text = """Hello, human! I am an excuse bot, built with Python and the Serverless Framework. I help with excuses {}.\n\
You can take a look at my source code here: https://github.com/vaibhavsingh97/serverless-chatbot-demo.\n\
Found a {}, please drop a tweet to my creator: https://twitter.com/vaibhavsingh97. Happy botting!""".format(emojize("! :laughing:", use_aliases=True), emojize("! :bug:", use_aliases=True))
bot.send_chat_action(chat_id=chat_id, action="TYPING")
time.sleep(2)
bot.send_message(chat_id=chat_id, text=text)
elif "missed" in text and "office" in text:
headers = {
"Accept": "application/json",
"User-Agent": "excuse bot (https://github.com/vaibhavsingh97/serverless-chatbot-demo)"
}
arr = excuseDict['missed office']
text = arr[random.randint(0, len(arr))]
bot.send_chat_action(chat_id=chat_id, action="TYPING")
time.sleep(2)
bot.send_message(chat_id=chat_id, text=text)
elif "forgot" in text and "birthday" in text:
headers = {
"Accept": "application/json",
"User-Agent": "excuse bot (https://github.com/vaibhavsingh97/serverless-chatbot-demo)"
}
arr = excuseDict['forgot birthday']
text = arr[random.randint(0, len(arr))]
bot.send_chat_action(chat_id=chat_id, action="TYPING")
time.sleep(2)
bot.send_message(chat_id=chat_id, text=text)
elif "forgot" in text and "anniversary" in text:
headers = {
"Accept": "application/json",
"User-Agent": "excuse bot (https://github.com/vaibhavsingh97/serverless-chatbot-demo)"
}
arr = excuseDict['forgot anniversary']
text = arr[random.randint(0, len(arr))]
bot.send_chat_action(chat_id=chat_id, action="TYPING")
time.sleep(2)
bot.send_message(chat_id=chat_id, text=text)
elif text == '/help':
text = """
Hello! Excuse bot welcomes you on the telegram!
Here's the commands:
- /start - to get know more about Excuse Bot
- /help - to view help text
This bot is being worked on, so it may break sometimes. Contact @vaibhavsingh97 on twitter \
or open issue [here](https://github.com/vaibhavsingh97/serverless-chatbot-demo).
"""
bot.send_chat_action(chat_id=chat_id, action="TYPING")
time.sleep(2)
bot.send_message(chat_id=chat_id, text=text, parse_mode="MARKDOWN")
logging.info("Message successfully sent")
# RESPONSE["OK"]["body"] = json.dumps("Message Sent")
return RESPONSE["OK"]
return RESPONSE["ERROR"]
def set_webhook(event, context):
logging.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:
RESPONSE["OK"]["body"] = json.dumps("Webhook URL successfully set.")
return RESPONSE["OK"]
return RESPONSE["ERROR"]
def get_webhook_info(event, context):
logging.info('Event: {}'.format(event))
bot = configure_telegram()
webhook_info = bot.get_webhook_info()
logging.info('Event: {}'.format(webhook_info))
if webhook_info:
RESPONSE["OK"]["body"] = str(webhook_info)
return RESPONSE["OK"]
return RESPONSE["ERROR"]