-
Notifications
You must be signed in to change notification settings - Fork 2
/
tg.py
81 lines (69 loc) · 2.79 KB
/
tg.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
import os
import requests
import random
import json
import csv
DATA = []
with open('moods.csv') as file:
cr = csv.DictReader(file)
for line in cr:
DATA.append(line)
API_TOKEN = os.environ['TG_API_TOKEN']
MOODS = [
'tired', 'party', 'exploring', 'holy'
]
LIMIT = 6
class Task:
chat_id = None
text = None
def __init__(self, chat_id, text):
self.chat_id = chat_id
self.text = text
def send_message(self, message):
req = requests.post(
'https://api.telegram.org/bot{token}/sendMessage'.format(token = API_TOKEN),
json={"chat_id": self.chat_id, "text": message}
)
return req.text
def send_location(self, lat, lng, rid):
req = requests.post(
'https://api.telegram.org/bot{token}/sendLocation'.format(token = API_TOKEN),
json={"chat_id": self.chat_id, "latitude": lat, "longitude": lng, "reply_to_message_id": rid}
)
return req.text
def start(self):
self.send_message("Trabot is simple bot for users to get travel choices based on whatever is on his/her mind or mood. You can choose between these moods: " + ', '.join(MOODS) + " for now.")
self.send_message("You can choose between the following commands for now: " + ", ".join(['/recommend_' + mood for mood in MOODS] + ['/start', '/city']))
def recommend(self, mood):
self.send_message('We will help you get started with your upcoming trip! You selected your mood as: ' + mood)
r_index = int(random.random() * LIMIT)
if mood == 'Exploring':
r_index = 0 + r_index
elif mood == 'Party':
r_index = 6 + r_index
elif mood == 'Tired':
r_index = 12 + r_index
elif mood == 'Holy':
r_index = 18 + r_index
name = DATA[r_index]['Name']
latitude = DATA[r_index]['Latitude']
longitude = DATA[r_index]['Longitude']
msg1 = json.loads(self.send_message(name))
msg1_id = msg1['result']['message_id']
self.send_location(latitude, longitude, msg1_id)
def city(self):
self.send_message("Once you plan your next city to travel, we'll assist you what will attract you there. :) ")
self.send_message("This feature is in beta stage and will be available soon.")
def do(self):
if self.text.startswith('/start'):
self.start()
elif self.text.startswith('/recommend_tired'):
self.recommend("Tired")
elif self.text.startswith('/recommend_party'):
self.recommend("Party")
elif self.text.startswith('/recommend_exploring'):
self.recommend("Exploring")
elif self.text.startswith('/recommend_holy'):
self.recommend("Holy")
elif self.text.startswith('/city'):
self.city()