''' A telegram bot api for whatever purposes. TODO: kaehmy app is definitely not correct place for this ''' import logging import requests 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 = "https://api.telegram.org/bot{}/sendMessage".format(self.api_token) 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)