8a8751ce66
Also send tg spam from ohlhafv challenges!
37 lines
1.2 KiB
Python
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) |