Files
web2.0-backend/kaehmy/tgbot.py
T
okalintu 8a8751ce66 Add barebones to better tgbot
Also send tg spam from ohlhafv challenges!
2019-02-08 18:26:06 +02:00

37 lines
1.2 KiB
Python

'''
A telegram bot api for whatever purposes.
TODO: kaehmy app is definitely not correct place for this
'''
import requests
import logging
from django.conf import settings
from kaehmy.models import TelegramChannel
class TelegramBot:
'''
A telegram bot api for whatever purposes
Currently only able to broadcast stuff to all registered
channels using broadcast method.
'''
def __init__(self, api_token=None):
self.api_token = api_token or settings.TELEGRAM_BOT_TOKEN
self.send_message_url = f'https://api.telegram.org/bot{self.api_token}/sendMessage'
def broadcast(self, message):
channels_ids = TelegramChannel.objects.values_list("channel_id", flat=True)
for id_ in channels_ids:
self.send_message(id_, message)
def send_message(self, channel_id, message):
'''
Send message to a chat with given channel_id
'''
data = {
'chat_id': channel_id,
'text': message,
'parse_mode': 'Markdown'
}
resp = requests.post(self.send_message_url, json=data)
logging.debug(resp.content)