-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
52 lines (42 loc) · 1.41 KB
/
main.py
File metadata and controls
52 lines (42 loc) · 1.41 KB
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
# encoding: utf-8
import json
from pprint import pprint
from time import sleep
import urllib
import urllib2
from config import TOKEN
class TelegramBot(object):
def __init__(self):
self.base_url = "https://api.telegram.org/bot" + TOKEN + "/"
self.offset = 0
def get_me(self):
response = json.load(urllib2.urlopen(self.base_url + "getMe"))
return response['ok']
def get_updates(self):
data = {
"offset": self.offset
}
response = json.load(urllib2.urlopen(self.base_url + "getUpdates", urllib.urlencode(data)))
if len(response['result']) > 0:
self.offset = response['result'][-1]['update_id'] + 1
return response['result']
def send_message(self, msg, chat_id, reply_id=None):
data = {
"chat_id": chat_id,
"text": msg,
}
if reply_id is not None:
data["reply_to_message_id"] = reply_id
response = json.load(urllib2.urlopen(self.base_url + "sendMessage", urllib.urlencode(data)))
#pprint(response)
def polling():
bot = TelegramBot()
print bot.get_me()
while True:
for in_message in bot.get_updates():
#pprint(in_message)
message = in_message['message']
bot.send_message(message['text'], message['chat']['id'], message['message_id'])
sleep(2)
if __name__ == "__main__":
polling()