-
Notifications
You must be signed in to change notification settings - Fork 2
/
bot.py
27 lines (21 loc) · 851 Bytes
/
bot.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
import requests
import json
import configparser as cfg
class telegram_chatbot():
def __init__(self, config):
self.token = self.read_token_from_config_file(config)
self.base = "https://api.telegram.org/bot{}/".format(self.token)
def get_updates(self, offset=None):
url = self.base + "getUpdates?timeout=100"
if offset:
url = url + "&offset={}".format(offset + 1)
r = requests.get(url)
return json.loads(r.content)
def send_message(self, msg, chat_id):
url = self.base + "sendMessage?chat_id={}&text={}".format(chat_id, msg)
if msg is not None:
requests.get(url)
def read_token_from_config_file(self, config):
parser = cfg.ConfigParser()
parser.read(config)
return parser.get('creds', 'token')