-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtelegram.py
More file actions
177 lines (137 loc) · 5.96 KB
/
telegram.py
File metadata and controls
177 lines (137 loc) · 5.96 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import requests
import json
import threading
import traceback
import utils
import plugin
from config import config
import plugins.ai_chat_handler as ai_chat_handler
class Telegram:
token: str
bot_id: int
update_id: int = -1
def __init__(self):
self.token = config.telegram_token
self.bot_id = self.sendMethod('getMe')['result']['id']
def start_thread(self):
telegram = utils.telegram
while True:
try:
for update in telegram.getUpdates():
msg = utils.Msg()
msg.parse_msg(update)
msg.parse_command()
if msg.skip: continue
if msg.is_command:
if msg.user.level < plugin.plugins_map[msg.command].level:
msg.sendMessage("У вас не достаточно прав для этой команды")
continue
thread = threading.Thread(
target=plugin.plugins_map[msg.command]().execute,
args=(msg,)
)
thread.start()
else:
if msg.reply_msg and msg.reply_msg.from_id == self.bot_id:
if msg.text == '': continue
msg.user_text = msg.text
context = ai_chat_handler.get_context(msg.reply_msg.msg_id)
if context: model = context['model']
else: model = 'gemini'
threading.Thread(
target=plugin.plugins_map[model]().execute,
args=(msg,)
).start()
except ValueError:
print(traceback.format_exc())
pass
def getUpdates(self):
result = requests.post(
'https://api.telegram.org/'+self.token+'/getUpdates',
data = {'offset':self.update_id},
timeout=30
).json()['result']
for update in result:
self.update_id = update['update_id']+1
if 'message' not in update: continue
yield update['message']
def getFile(self, file_id):
file_path = self.sendMethod("getFile", file_id = file_id)['result']['file_path']
url = f'https://api.telegram.org/file/{self.token}/{file_path}'
response = requests.get(url, stream=True)
return response
def getPhoto(self, photo_list):
return self.getFile(self.getMaxPhoto(photo_list))
def getMaxPhoto(self, photo_list):
max = 0
max_photo = None
for photo in photo_list:
if photo['file_size'] > max:
max = photo['file_size']
max_photo = photo['file_id']
return max_photo
def sendPhotos(self, chat_id, data, **parameters):
parameters['chat_id'] = chat_id
media_group = []
files = {}
for media in data:
if type(media) == bytes:
file_name = f'photo{len(files)}'
#files.append(('photo', (file_name, media, 'multipart/form-data')))
files[file_name] = media
media_group.append({
'type': 'photo',
'media': f'attach://{file_name}'
})
if type(media) == str:
media_group.append({
'type': 'photo',
'media': media
})
if len(media_group) > 0:
media_group[0]['caption'] = parameters['caption']
del parameters['caption']
parameters['media'] = json.dumps(media_group)
result = requests.post(f'https://api.telegram.org/{self.token}/sendMediaGroup', data=parameters, files=files).json()
return result
def sendPhoto(self, chat_id: int, reply_id: int, data, **parameters):
parameters['chat_id'] = chat_id
files = None
photo = None
if type(data) == bytes:
files = {'photo':('photo', data, 'multipart/form-data')}
if type(data) == str:
parameters['photo'] = photo
if reply_id != None:
parameters['reply_to_message_id'] = reply_id
parameters['allow_sending_without_reply'] = True
result = requests.post(f'https://api.telegram.org/{self.token}/sendPhoto', params=parameters, files=files).json()
return result
def sendAudio(self, chat_id: int, reply_id: int, data, **parameters):
parameters['chat_id'] = chat_id
files = None
photo = None
if type(data) == bytes:
files = {'audio':(parameters['name'], data, 'multipart/form-data')}
if type(data) == str:
parameters['audio'] = photo
if reply_id != None:
parameters['reply_to_message_id'] = reply_id
parameters['allow_sending_without_reply'] = True
result = requests.post(f'https://api.telegram.org/{self.token}/sendAudio', params=parameters, files=files).json()
return result
def sendMessage(self, text: str, chat_id: int, reply_id: int, attachments: list, **parameters):
parameters['chat_id'] = chat_id
if text == '': text = ' '
parameters['text'] = text
if reply_id != None:
parameters['reply_to_message_id'] = reply_id
parameters['allow_sending_without_reply'] = True
result = self.sendMethod("sendMessage", **parameters)
return result
def sendMethod(self, method, **parameters):
result = requests.post(f'https://api.telegram.org/{self.token}/{method}', params=parameters).json()
if 'error_code' in result:
print(result)
#sendMessage("Ошибка: ${result.getInt("error_code")}\n${result.getString("description")}", chatId)
return result