-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbot.py
60 lines (43 loc) · 1.45 KB
/
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
54
55
56
57
58
59
60
import json
import requests
import time
TOKEN = "insert_token"
URL = "https://api.telegram.org/bot{}/".format(TOKEN)
def check_connection(url):
try:
_ = requests.get(url, timeout=5)
return True
except requests.ConnectionError:
print("No connection")
return False
def get_url(url):
response = requests.get(url)
content = response.content.decode("utf8")
return content
def get_json(url):
content = get_url(url)
js = json.loads(content)
return js
def get_updates():
url = URL + "getUpdates"
js = get_json(url)
return js
def get_users(updates):
# We get the id's of every user to send them messages
num_updates = len(updates["result"])
id_list = list(set([updates["result"][i]["message"]["chat"]["id"] for i in range(num_updates)]))
return id_list
def broadcast(id_list, message):
# Send the message
for user_id in id_list:
url = URL + "sendMessage?text={}&chat_id={}".format(message, user_id)
get_url(url)
def send_message(user_id, message):
url = URL + "sendMessage?text={}&chat_id={}".format(message, user_id)
get_url(url)
def show_bets(updates, text):
last_text = updates["result"][-1]["message"]["text"]
user_id = updates["result"][-1]["message"]["chat"]["id"]
if last_text == "/kupon" and len(updates["result"]) > 0:
send_message(user_id, text)
time.sleep(15)