telegram_bots/bots/modules/channel_helper.py

131 lines
4.6 KiB
Python
Raw Normal View History

2019-11-12 17:13:56 +00:00
import base64
2019-11-24 16:22:30 +00:00
import json
2019-11-12 17:13:56 +00:00
import os
import tempfile
from io import BytesIO
from uuid import uuid4
import requests
from PIL import Image
from django.db import models
2019-11-24 16:22:30 +00:00
from telegram import Update, Bot
2019-11-12 17:13:56 +00:00
from telegram.ext import Dispatcher, CallbackContext, MessageHandler, Filters
from jsonrpc import Dispatcher as RPCDispatcher
2019-11-24 16:22:30 +00:00
from djconfig import config
2019-11-12 17:13:56 +00:00
from bots.models import TelegramBotModuleConfig
class ChannelHelperBotModuleConfig(TelegramBotModuleConfig):
chat_id = models.CharField(max_length=32)
2019-11-24 16:22:30 +00:00
queued = models.BooleanField(default=False)
2019-11-12 17:13:56 +00:00
MODULE_NAME = 'Channel helper'
rpc_dispatcher = None
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.rpc_dispatcher = RPCDispatcher()
self.rpc_dispatcher['post_photo'] = self.rpc_post_photo
def rpc_post_photo(self, photo, is_base64=False):
2019-11-24 16:22:30 +00:00
config._reload_maybe()
bot = self.bot.get_bot()
2019-11-12 17:13:56 +00:00
try:
if is_base64:
f = BytesIO(base64.b64decode(photo))
else:
resp = requests.get(photo)
resp.raise_for_status()
f = BytesIO(resp.content)
except:
raise RuntimeError('Could not load image')
im = Image.open(f) # type: Image.Image
width, height = im.size
if width > 2000 or height > 2000:
im.thumbnail((2000, 2000))
2019-11-12 17:52:08 +00:00
im = im.convert('RGB')
2019-11-12 17:13:56 +00:00
with tempfile.TemporaryDirectory() as d:
fpath = os.path.join(d, '{}.jpg'.format(uuid4()))
2019-11-12 17:52:08 +00:00
im.save(fpath)
2019-11-24 16:42:28 +00:00
if self.queued:
m = bot.send_photo(config.tmp_uploads_chat_id, open(fpath, 'rb'))
i = QueuedItem(config=self, type='photo', args=json.dumps([m.photo[-1].file_id]))
i.save()
else:
bot.send_photo(self.chat_id, open(fpath, 'rb'))
2019-11-12 17:13:56 +00:00
return True
2019-11-24 16:22:30 +00:00
def periodic_task(self, bot: Bot):
i = self.queued_items.order_by('?').first() # type: QueuedItem
if i:
i.send(bot)
i.delete()
2019-11-12 17:13:56 +00:00
def handle_message(self, update: Update, ctx: CallbackContext):
m = update.effective_message
bot = ctx.bot
2019-11-24 16:22:30 +00:00
i = QueuedItem(config=self)
2019-11-12 17:13:56 +00:00
if hasattr(m, 'audio') and m.audio:
a = m.audio
2019-11-24 16:22:30 +00:00
i.type = 'audio'
i.args = json.dumps([a.file_id, a.duration, a.performer, a.title])
2019-11-12 17:13:56 +00:00
elif hasattr(m, 'document') and m.document:
d = m.document
2019-11-24 16:22:30 +00:00
i.type = 'document'
i.args = json.dumps([d.file_id, d.file_name])
2019-11-12 17:13:56 +00:00
elif hasattr(m, 'photo') and m.photo:
p = m.photo
2019-11-24 16:22:30 +00:00
i.type = 'photo'
i.args = json.dumps([p[-1].file_id])
2019-11-12 17:13:56 +00:00
elif hasattr(m, 'sticker') and m.sticker:
s = m.sticker
2019-11-24 16:22:30 +00:00
i.type = 'sticker'
i.args = json.dumps([s.file_id])
2019-11-12 17:13:56 +00:00
elif hasattr(m, 'video') and m.video:
v = m.video
2019-11-24 16:22:30 +00:00
i.type = 'video'
i.args = json.dumps([v.file_id, v.duration])
2019-11-12 17:13:56 +00:00
elif hasattr(m, 'voice') and m.voice:
v = m.voice
2019-11-24 16:22:30 +00:00
i.type = 'voice'
i.args = json.dumps([v.file_id, v.duration])
2019-11-12 17:13:56 +00:00
elif hasattr(m, 'video_note') and m.video_note:
vn = m.video_note
2019-11-24 16:22:30 +00:00
i.type = 'video_note'
i.args = json.dumps([vn.file_id, vn.duration, vn.length])
2019-11-12 17:13:56 +00:00
elif hasattr(m, 'contact') and m.contact:
c = m.contact
2019-11-24 16:22:30 +00:00
i.type = 'contact'
i.args = json.dumps([c.phone_number, c.first_name, c.last_name])
2019-11-12 17:13:56 +00:00
elif hasattr(m, 'location') and m.location:
l = m.location
2019-11-24 16:22:30 +00:00
i.type = 'location'
i.args = json.dumps([l.latitude, l.longitude])
2019-11-12 17:13:56 +00:00
elif hasattr(m, 'venue') and m.venue:
v = m.venue
2019-11-24 16:22:30 +00:00
i.type = 'venue'
i.args = json.dumps([v.location.latitude, v.location.longitude, v.title, v.address, v.foursquare_id])
2019-11-12 17:13:56 +00:00
elif hasattr(m, 'text') and m.text:
2019-12-04 15:38:14 +00:00
i.type = 'message'
2019-11-24 16:22:30 +00:00
i.args = json.dumps([m.text_html, 'html'])
if self.queued:
i.save()
else:
i.send(bot)
2019-11-12 17:13:56 +00:00
def build_dispatcher(self, dispatcher: Dispatcher):
2019-11-12 17:25:23 +00:00
dispatcher.add_handler(MessageHandler(Filters.private, self.handle_message))
2019-11-12 17:13:56 +00:00
return dispatcher
2019-11-24 16:22:30 +00:00
class QueuedItem(models.Model):
config = models.ForeignKey(ChannelHelperBotModuleConfig, on_delete=models.CASCADE, related_name='queued_items')
type = models.CharField(max_length=12)
args = models.TextField()
def send(self, bot: Bot):
getattr(bot, 'send_' + self.type)(self.config.chat_id, *json.loads(self.args))