-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtelegrambot.py
85 lines (65 loc) · 2.42 KB
/
telegrambot.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
import json
import requests
import time
import urllib
import random
#import config
# edite aqui com seu token
TOKEN = "305103765:AAG5YuDFCBOES4mRRqQzkzkJcqfxEr9bom4"
#TOKEN = config.token
URL = "https://api.telegram.org/bot{}/".format(TOKEN)
imagens=[ "http://cdn3-www.cattime.com/assets/uploads/2011/08/best-kitten-names-1.jpg",
"http://www.pets4homes.co.uk/images/articles/1646/large/kitten-emergencies-signs-to-look-out-for-537479947ec1c.jpg",
"http://dreamatico.com/data_images/kitten/kitten-1.jpg", "https://pbs.twimg.com/profile_images/562466745340817408/_nIu8KHX.jpeg",
"http://dreamatico.com/data_images/kitten/kitten-3.jpg",
"https://upload.wikimedia.org/wikipedia/commons/thumb/0/06/Kitten_in_Rizal_Park,_Manila.jpg/230px-Kitten_in_Rizal_Park,_Manila.jpg"]
def get_url(url):
response = requests.get(url)
content = response.content.decode("utf8")
return content
def get_json_from_url(url):
content = get_url(url)
js = json.loads(content)
return js
def get_updates(offset=None):
url = URL + "getUpdates"
if offset:
url += "?offset={}".format(offset)
js = get_json_from_url(url)
return js
def get_last_update_id(updates):
update_ids = []
for update in updates["result"]:
update_ids.append(int(update["update_id"]))
return max(update_ids)
def echo_all(updates):
for update in updates["result"]:
text = update["message"]["text"]
chat = update["message"]["chat"]["id"]
send_message(text, chat)
def get_last_chat_id_and_text(updates):
num_updates = len(updates["result"])
last_update = num_updates - 1
text = updates["result"][last_update]["message"]["text"]
chat_id = updates["result"][last_update]["message"]["chat"]["id"]
return (text, chat_id)
def send_message(text, chat_id):
text = urllib.parse.quote_plus(text)
url = URL + "sendMessage?text={}&chat_id={}".format(text, chat_id)
get_url(url)
def send_kittens(updates):
for update in updates["result"]:
text = imagens[random.randint(0,5)]
chat = update["message"]["chat"]["id"]
send_message(text, chat)
def main():
last_update_id = None
while True:
updates = get_updates(last_update_id)
if len(updates["result"]) > 0:
last_update_id = get_last_update_id(updates) + 1
#echo_all(updates)
send_kittens(updates)
time.sleep(0.5)
if __name__ == '__main__':
main()