-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtelegbot.py
More file actions
105 lines (91 loc) · 3.81 KB
/
Copy pathtelegbot.py
File metadata and controls
105 lines (91 loc) · 3.81 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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/python
import urllib.request
import json
import yaml
import string
class telegbot:
def __init__(self):
self.config = yaml.load(open("config.yml", 'r'))
self.data = self.getBotData()
self.on_receive_message = self.void_callback
self.on_new_chat_participant = self.void_callback
self.quit = False
print(self.data)
def void_callback(self, data={}):
return
def apiRequest(self, method, parameters = {}):
if not self.methodExists(method):
return False
url = self.config["telegramBotApi"]["api_url"]
url = url.replace('{token}', self.getBotToken())
url = url.replace('{method}', method)
values = self.manageParameters(method, parameters)
if values == None:
return False
data = urllib.parse.urlencode(values)
data = data.encode('utf-8') # data should be bytes
req = urllib.request.Request(url, data)
with urllib.request.urlopen(req) as response:
return json.loads(response.read().decode())
#the_page = response.read().decode()
#return json.dumps(json.loads(the_page))
return False
def manageParameters(self, method, parameters):
managedParams = {}
if not self.methodExists(method):
return False
if self.config["telegramBotApi"]["methods"][method]["parameters"] == None:
return managedParams
for methodParameter in self.config["telegramBotApi"]["methods"][method]["parameters"]:
methodParameterData = self.config["telegramBotApi"]["methods"][method]["parameters"][methodParameter]
if methodParameter in parameters:
if (parameters[methodParameter] == None and not methodParameterData["required"]):
continue
if not (methodParameterData["type"] == type(parameters[methodParameter]).__name__):
return False
managedParams[methodParameterData["parameter"]] = parameters[methodParameter]
else:
if methodParameter["required"]:
return False
return managedParams
def methodExists(self, method):
return method in self.config["telegramBotApi"]["methods"]
def getBotToken(self):
return self.config["botData"]["token"]
def getBotUsername(self):
return self.data["username"]
def getBotData(self):
botData = self.apiRequest('getMe')
if not botData:
return False
return botData["result"]
def sendMessage(self, chat_id, text, disable_web_page_preview=False, reply_to_message_id=None, reply_markup=None):
response = self.apiRequest('sendMessage', {
"chat_id": chat_id,
"text": text,
"disable_web_page_preview": disable_web_page_preview,
"reply_to_message_id": reply_to_message_id,
"reply_markup": reply_markup
})
if response["ok"]:
self.runEvent(response["result"])
def runEvent(self, event):
if "text" in event:
self.on_receive_message(event)
elif "new_chat_participant" in event:
self.on_new_chat_participant(event)
else:
print(response)
def run(self):
lastMessage_update_id = 0
while (not self.quit):
response = self.apiRequest('getUpdates', {
"offset": lastMessage_update_id + 1,
"limit": 100,
"timeout": 20
})
if response["ok"]:
for update in response["result"]:
if update["update_id"] > lastMessage_update_id:
lastMessage_update_id = update["update_id"]
self.runEvent(update["message"])