Repository navigation
Expand file tree
/
Copy path__init__.py
More file actions
201 lines (176 loc) · 6.58 KB
/
Copy path__init__.py
File metadata and controls
201 lines (176 loc) · 6.58 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# -*- coding: utf-8 -*-
from __future__ import print_function
import json
import time
import threading
from telebot import apihelper, types
"""
Module : telebot
"""
API_URL = r"https://api.telegram.org/"
class TeleBot:
""" This is TeleBot Class
Methods:
getMe
sendMessage
forwardMessage
sendPhoto
sendAudio
sendDocument
sendSticker
sendVideo
sendLocation
sendChatAction
getUserProfilePhotos
getUpdates
setWebhook
"""
def __init__(self, token):
self.token = token
self.update_listener = []
self.polling_thread = None
self.__stop_polling = False
self.interval = 3
self.last_update_id = 0
def get_update(self):
result = apihelper.get_updates(self.token, offset=(self.last_update_id + 1))
updates = result['result']
notify_updates = []
for update in updates:
if update['update_id'] > self.last_update_id:
self.last_update_id = update['update_id']
msg = types.Message.de_json(json.dumps(update['message']))
notify_updates.append(msg)
self.__notify_update(notify_updates)
def __notify_update(self, new_messages):
for listener in self.update_listener:
t = threading.Thread(target=listener, args=new_messages)
t.start()
def polling(self, interval=3):
"""
Always get updates.
:param interval: iterval secs.
:return:
"""
self.interval = interval
# clear thread.
self.__stop_polling = True
time.sleep(interval + 1)
self.__stop_polling = False
self.polling_thread = threading.Thread(target=self.__polling, args=())
self.polling_thread.daemon = True
self.polling_thread.start()
def __polling(self):
print('telegram bot start polling')
while not self.__stop_polling:
try:
self.get_update()
except Exception as e:
print(e)
time.sleep(self.interval)
print('telegram bot stop polling')
def stop_polling(self):
self.__stop_polling = True
def set_update_listener(self, listener):
self.update_listener.append(listener)
def get_me(self):
result = apihelper.get_me(self.token)
if result['ok'] is not True:
raise Exception('getMe Error.' + json.dumps(result))
u = types.User.de_json(json.dumps(result['result']))
return u
def send_message(self, chat_id, text, disable_web_page_preview=None, reply_to_message_id=None, reply_markup=None):
"""
Use this method to send text messages.
:param chat_id:
:param text:
:param disable_web_page_preview:
:param reply_to_message_id:
:param reply_markup:
:return:
"""
return apihelper.send_message(self.token, chat_id, text, disable_web_page_preview, reply_to_message_id,
reply_markup)
def forward_message(self, chat_id, from_chat_id, message_id):
"""
Use this method to forward messages of any kind.
:param chat_id: which chat to forward
:param from_chat_id: which chat message from
:param message_id: message id
:return:
"""
return apihelper.forward_message(self.token, chat_id, from_chat_id, message_id)
def send_photo(self, chat_id, photo, caption=None, reply_to_message_id=None, reply_markup=None):
"""
Use this method to send photos.
:param chat_id:
:param photo:
:param caption:
:param reply_to_message_id:
:param reply_markup:
:return:
"""
return apihelper.send_photo(self.token, chat_id, photo, caption, reply_to_message_id, reply_markup)
def send_audio(self, chat_id, data, reply_to_message_id=None, reply_markup=None):
"""
Use this method to send audio files, if you want Telegram clients to display the file as a playable
voice message. For this to work, your audio must be in an .ogg file encoded with OPUS
:param chat_id:
:param data:
:param reply_to_message_id:
:param reply_markup:
:return:
"""
return apihelper.send_data(self.token, chat_id, data, 'audio', reply_to_message_id, reply_markup)
def send_document(self, chat_id, data, reply_to_message_id=None, reply_markup=None):
"""
Use this method to send general files.
:param chat_id:
:param data:
:param reply_to_message_id:
:param reply_markup:
:return:
"""
return apihelper.send_data(self.token, chat_id, data, 'document', reply_to_message_id, reply_markup)
def send_sticker(self, chat_id, data, reply_to_message_id=None, reply_markup=None):
"""
Use this method to send .webp stickers.
:param chat_id:
:param data:
:param reply_to_message_id:
:param reply_markup:
:return:
"""
return apihelper.send_data(self.token, chat_id, data, 'sticker', reply_to_message_id, reply_markup)
def send_video(self, chat_id, data, reply_to_message_id=None, reply_markup=None):
"""
Use this method to send video files, Telegram clients support mp4 videos.
:param chat_id:
:param data:
:param reply_to_message_id:
:param reply_markup:
:return:
"""
return apihelper.send_data(self.token, chat_id, data, 'video', reply_to_message_id, reply_markup)
def send_location(self, chat_id, latitude, longitude, reply_to_message_id=None, reply_markup=None):
"""
Use this method to send point on the map.
:param chat_id:
:param latitude:
:param longitude:
:param reply_to_message_id:
:param reply_markup:
:return:
"""
return apihelper.send_location(self.token, chat_id, latitude, longitude, reply_to_message_id, reply_markup)
def send_chat_action(self, chat_id, action):
"""
Use this method when you need to tell the user that something is happening on the bot's side.
The status is set for 5 seconds or less (when a message arrives from your bot, Telegram clients clear
its typing status).
:param chat_id:
:param action: string . typing,upload_photo,record_video,upload_video,record_audio,upload_audio,upload_document,
find_location.
:return:
"""
return apihelper.send_chat_action(self.token, chat_id, action)