forked from furas/python-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathecho_bot.py
More file actions
49 lines (40 loc) · 1.58 KB
/
Copy pathecho_bot.py
File metadata and controls
49 lines (40 loc) · 1.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
#!/usr/bin/env python3
import os
import requests
import time
from pprint import pprint
#token = '<MY TOKEN>'
token = os.environ['TELEGRAM_TOKEN']
print('Running ...')
# to skip older messages
update_id = 0
while True:
# get messages
url = f'https://api.telegram.org/bot{token}/getUpdates'
r = requests.get(url, params={'offset': update_id}) # `offset` to skip older messages
data = r.json()
if not data['ok']:
print('ERROR:', data)
else:
for item in data['result']:
update_id = item['update_id'] + 1 # to skip older messages
message = item.get('message')
if message:
if 'text' in message:
chat_id = message['chat']['id']
url = f'https://api.telegram.org/bot{token}/sendMessage'
r = requests.get(url, params={'chat_id': chat_id, 'text': message['text']})
print('response:', r.json())
if 'photo' in message:
chat_id = message['chat']['id']
url = f'https://api.telegram.org/bot{token}/sendPhoto'
r = requests.get(url, params={'chat_id': chat_id, 'photo': message['photo'][0]['file_id']})
print('response:', r.json())
if 'voice' in message:
print('voice:')
pprint(message['voice'])
# etc.
else: # it can be ie. `edited_message`
print('item:')
pprint(item)
time.sleep(1)