89 lines
3.4 KiB
Python
89 lines
3.4 KiB
Python
import base64
|
|
import os
|
|
import tempfile
|
|
from io import BytesIO
|
|
from uuid import uuid4
|
|
|
|
import requests
|
|
from PIL import Image
|
|
from django.db import models
|
|
from telegram import Update
|
|
from telegram.ext import Dispatcher, CallbackContext, MessageHandler, Filters
|
|
from jsonrpc import Dispatcher as RPCDispatcher
|
|
|
|
from bots.models import TelegramBotModuleConfig
|
|
|
|
|
|
class ChannelHelperBotModuleConfig(TelegramBotModuleConfig):
|
|
chat_id = models.CharField(max_length=32)
|
|
|
|
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):
|
|
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))
|
|
im = im.convert('RGB')
|
|
with tempfile.TemporaryDirectory() as d:
|
|
fpath = os.path.join(d, '{}.jpg'.format(uuid4()))
|
|
im.save(fpath)
|
|
self.bot.get_bot().send_photo(self.chat_id, open(fpath, 'rb'))
|
|
return True
|
|
|
|
def handle_message(self, update: Update, ctx: CallbackContext):
|
|
m = update.effective_message
|
|
bot = ctx.bot
|
|
if hasattr(m, 'audio') and m.audio:
|
|
a = m.audio
|
|
r = bot.send_audio(self.chat_id, a.file_id, a.duration, a.performer, a.title)
|
|
elif hasattr(m, 'document') and m.document:
|
|
d = m.document
|
|
r = bot.send_document(self.chat_id, d.file_id, d.file_name)
|
|
elif hasattr(m, 'photo') and m.photo:
|
|
p = m.photo
|
|
r = bot.send_photo(self.chat_id, p[-1].file_id)
|
|
elif hasattr(m, 'sticker') and m.sticker:
|
|
s = m.sticker
|
|
r = bot.send_sticker(self.chat_id, s.file_id)
|
|
elif hasattr(m, 'video') and m.video:
|
|
v = m.video
|
|
r = bot.send_video(self.chat_id, v.file_id, v.duration)
|
|
elif hasattr(m, 'voice') and m.voice:
|
|
v = m.voice
|
|
r = bot.send_voice(self.chat_id, v.file_id, v.duration)
|
|
elif hasattr(m, 'video_note') and m.video_note:
|
|
vn = m.video_note
|
|
r = bot.send_video_note(self.chat_id, vn.file_id, vn.duration, vn.length)
|
|
elif hasattr(m, 'contact') and m.contact:
|
|
c = m.contact
|
|
r = bot.send_contact(self.chat_id, c.phone_number, c.first_name, c.last_name)
|
|
elif hasattr(m, 'location') and m.location:
|
|
l = m.location
|
|
r = bot.send_location(self.chat_id, l.latitude, l.longitude)
|
|
elif hasattr(m, 'venue') and m.venue:
|
|
v = m.venue
|
|
r = bot.send_venue(self.chat_id, v.location.latitude, v.location.longitude, v.title, v.address, v.foursquare_id)
|
|
elif hasattr(m, 'text') and m.text:
|
|
r = bot.send_message(self.chat_id, m.text_html, 'html')
|
|
|
|
def build_dispatcher(self, dispatcher: Dispatcher):
|
|
dispatcher.add_handler(MessageHandler(Filters.private, self.handle_message))
|
|
return dispatcher
|