-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_operations.py
executable file
·69 lines (55 loc) · 1.86 KB
/
api_operations.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
import json
import requests
import urllib
# token to access api
TOKEN = <TOKEN>
# base url for Telegram api
URL = "https://api.telegram.org/bot{}/".format(TOKEN)
# gets the content from the url
def get_url(url):
try:
response = requests.get(url)
content = response.content.decode("utf8")
return content
except requests.exceptions.RequestException as e:
print(e)
return -1
# converts json content to dictionary
def get_json_from_url(url):
content = get_url(url)
if content != -1:
js = json.loads(content)
return js
else:
return -1
# retrieves the messages sent to the bot
def get_updates(offset=None):
url = URL + "getUpdates?timeout=100"
if offset:
url += "&offset={}".format(offset)
js = get_json_from_url(url)
if js != -1:
return js
else:
return -1
# gets the id of the message that is sent most recently
def get_last_update_id(updates):
update_ids = []
for update in updates["result"]:
update_ids.append(int(update["update_id"]))
return max(update_ids)
# builds the customized keyboard
def build_keyboard(items, command, indicator, updt_val=""):
if indicator == 0:
keyboard = [[command + item[0] + " " + item[1] + " " + updt_val] for item in items]
else:
keyboard = [[command + item + " " + updt_val] for item in items]
reply_markup = {"keyboard":keyboard, "one_time_keyboard": True}
return json.dumps(reply_markup)
# sends a message from bot
def send_message(text, chat_id, reply_markup=None):
text = urllib.parse.quote_plus(text)
url = URL + "sendMessage?text={}&chat_id={}&parse_mode=Markdown".format(text, chat_id)
if reply_markup:
url += "&reply_markup={}".format(reply_markup)
get_url(url)