-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtelegram.py
62 lines (47 loc) · 1.55 KB
/
telegram.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
from requests import post, get
from requests.exceptions import RequestException
from datetime import datetime as time
from config import token
import logging as log
from time import sleep
def logg(text):
with open('data/json.log', 'a') as f:
f.write(str(time.now()) + " " + str(text) + '\n')
def api(method, request, data=None):
url = 'https://api.telegram.org/bot' + token + '/' + method
log.debug(f'request: {method} {data}')
while True:
try:
if request == post:
resp = request(url, json=data).json()
elif request == get:
if data:
url += data
resp = request(url).json()
break
except RequestException as e:
log.error(e)
sleep(1)
continue
# if not {'ok': True, 'result': []}
if not resp.get('ok'):
log.error(resp)
elif not resp['result']:
log.debug('no updates')
return resp
def get_updates(timeout=0, offset=None):
data = '?timeout=' + str(timeout)
if offset:
data += '&offset=' + str(offset)
return api('getUpdates', get, data)
def send(id, text, parse_mode=None):
dic = {'chat_id': id, 'text': text}
if parse_mode:
dic['parse_mode'] = parse_mode
return api('sendMessage', post, dic)
def chat_title(id, title):
dic = {'chat_id': id, 'title': title}
return api('setChatTitle', post, dic)
def delete(chat, message):
dic = {'chat_id': chat, 'message_id': message}
return api('deleteMessage', post, dic)