-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
87 lines (63 loc) · 2.33 KB
/
app.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
import os
import random
import json
import configparser
import requests
import telepot
from telepot.exception import TelegramError
from emoji import emojize
# Set Keys from Config
config = configparser.ConfigParser()
config.sections()
config.read('./enviroment.ini')
shiokapi_url = config['development']['SHIOK_API_HTTP']
channel_id = config['development']['CHANNEL_ID']
# The main URL for the Telegram API with our bot's token
bot = telepot.Bot(os.environ.get('TELEGRAM_TOKEN'))
def ask_shiokapi(endpoint):
""" helper function to get results from shiokapi """
return json.loads(requests.get(shiokapi_url + endpoint).text)
def personality(topic):
""" helper function to get shiokbot's personality! """
personailty = list(json.load(open("personality.json"))[topic])
return emojize(random.choice(personailty), use_aliases=True)
""" All the command and chat handlers """
def start(chat_id):
""" Start Command """
bot.sendMessage(chat_id, text='Hi! I am a Telegram Bot!!')
def weather(chat_id):
""" Weather Command """
bot.sendMessage(chat_id, text=personality("weather"))
bot.sendChatAction(chat_id, "typing")
weather1 = ask_shiokapi("weather")
weather2 = ask_shiokapi("weatherwarning")
weather3 = ask_shiokapi("weathermap")
bot.sendMessage(chat_id, text=weather2, parse_mode="HTML")
bot.sendMessage(chat_id, text=weather1, parse_mode="HTML")
bot.sendPhoto(chat_id, photo=weather3)
def read_message(message):
""" Returns Message as Dictionary """
if isinstance(message, dict):
message = message['message']
else:
message = json.loads(message)['message']
return message
def handle(message):
""" Handle JSON Message from Telegram
message - string
http://telepot.readthedocs.io/en/latest/reference.html
"""
message = read_message(message)
content_type, chat_type, chat_id = telepot.glance(message)
try:
if message['text'] == '/start':
response = "/start"
start(chat_id)
elif message['text'] == '/weather':
response = "/weather"
weather(chat_id)
else:
response = "_NOACTION_"
except TelegramError:
print("If this is the local enviroment then all is good ;)")
return {'content_type':content_type, 'chat_id':chat_id, 'response': response}