-
Notifications
You must be signed in to change notification settings - Fork 0
/
telegram_bot.py
53 lines (46 loc) · 1.4 KB
/
telegram_bot.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
import os
import boto3
import telegram
import json
from random import choice
try:
TOKEN = os.environ["TOKEN"]
except KeyError:
raise RuntimeError("TOKEN not set")
NOT_ANGERY = ["arranger", "danger", "hanger", "manger", "ranger", "tangerine"]
ANGERY_SUPPRESSOR = ["no_react", "noreact", "nobot", "no_bot"] + NOT_ANGERY
ANGERY_TRIGGER = (
"angery",
"angry",
"anger",
":frowning:",
":anger:",
":angry:",
"aggravating",
)
ANGERY_RESOURCES = os.path.join("resources", "angery.txt")
BOT = telegram.Bot(token=TOKEN)
def lambda_handler(event, context):
body = json.loads(event["body"])
update = telegram.Update.de_json(body, BOT)
if not update or not update.message or not update.message.text:
return {}
search_space = update.message.text.lower()
if not (
search_space
and not any(suppressor in search_space for suppressor in ANGERY_SUPPRESSOR)
and any(trigger in search_space for trigger in ANGERY_TRIGGER)
):
return {}
with open(ANGERY_RESOURCES, "r") as f:
imgs = f.readlines()
img = choice(imgs)[:-1]
update.message.reply_photo(img)
write_angery_metric()
return {}
def write_angery_metric():
cloudwatch = boto3.client("cloudwatch")
response = cloudwatch.put_metric_data(
Namespace="AngeryBot",
MetricData=[{"MetricName": "AngeryServed", "Unit": "Count", "Value": 1}],
)