-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda.py
More file actions
86 lines (70 loc) · 3.14 KB
/
lambda.py
File metadata and controls
86 lines (70 loc) · 3.14 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from telegram.ext import Updater, CommandHandler
import requests
import boto3
from bs4 import BeautifulSoup
token = <BOTTOKEN>
# Extracting the necessary information from the website
def get_info():
page = requests.get('https://www.jma.go.jp/en/quake/')
bs_page = BeautifulSoup(page.text, 'html.parser')
datetime = bs_page.find(attrs={'class': 'textframe'}).findAll('td')[6]
magnitude = bs_page.find(attrs={'class': 'textframe'}).findAll('td')[10]
region = bs_page.find(attrs={'class': 'textframe'}).findAll('td')[11]
return datetime.text, magnitude.text, region.text
# Basic message sender for the 'help' command
def eq_message(bot, update):
datetime, magnitude, region = get_info()
chat_id = update.message.chat_id
text = "New earthquake!\n{} with {} magnitude, in {}.".format(datetime, magnitude, region)
bot.send_message(chat_id=chat_id, text=text)
# Function for the 'add' command, subscribing the user by adding the chat_id to the db
def subscribe(bot, update):
chat_id = update.message.chat_id
dynamodb = boto3.client('dynamodb')
try:
dynamodb.put_item(TableName='subscribers', Item={'chat_id': {'N': str(chat_id)}})
except Exception as e:
print(e)
bot.send_message(chat_id=chat_id, text="You have subscribed!")
# Function for the 'delete' command, unsubscribe the user by deleting her chat_id from the db
def unsubscribe(bot, update):
chat_id = update.message.chat_id
dynamodb = boto3.client('dynamodb')
try:
dynamodb.delete_item(TableName='subscribers', Key={'chat_id': {'N': str(chat_id)}})
except Exception as e:
print(e)
bot.send_message(chat_id=chat_id, text="You have unsubscribed!")
# send notification to the chat_ids in the db, in case there is a new earthquake
def send_notification(bot):
# collect all chat_ids
dynamodb = boto3.client('dynamodb')
ids = dynamodb.scan(TableName='subscribers')
# collect the actual information
datetime, magnitude, region = get_info()
eq_info = datetime+magnitude+region
# collect the last information sent
old_info = dynamodb.scan(TableName='earthquake_info')
old_info = old_info['Items'][0]['info']['S']
# if the information is new, send a notification to all the chat_ids in the db
if not old_info == eq_info:
dynamodb.delete_item(TableName='earthquake_info', Key={'info': {'S': str(old_info)}})
dynamodb.put_item(TableName='earthquake_info', Item={'info': {'S': str(eq_info)}})
text = "New earthquake!\n{} with {} magnitude, in {}.".format(datetime, magnitude, region)
for item in ids['Items']:
id_number = item['chat_id']['N']
try:
bot.send_message(chat_id=id_number, text=text)
except Exception as e:
print(e)
# lambda handler for aws
def lambda_handler(event, context):
updater = Updater(token)
dp = updater.dispatcher
bot = updater.bot
dp.add_handler(CommandHandler('last', eq_message))
dp.add_handler(CommandHandler('add', subscribe))
dp.add_handler(CommandHandler('delete', unsubscribe))
updater.start_polling()
send_notification(bot)
updater.idle()