From 7f35062d33bfe76b1410bc0c5f647f07e13c0cdc Mon Sep 17 00:00:00 2001 From: luotuan Date: Sun, 26 Feb 2023 17:56:00 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8B=E8=BD=BD=E5=9B=BE=E7=89=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- channel/telegram/telegram_channel.py | 49 ++++++++++++++++++++++++---- config-template.json | 3 ++ 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/channel/telegram/telegram_channel.py b/channel/telegram/telegram_channel.py index 6d579a8..3b8ced7 100644 --- a/channel/telegram/telegram_channel.py +++ b/channel/telegram/telegram_channel.py @@ -3,15 +3,17 @@ from concurrent.futures import ThreadPoolExecutor import io import requests import telebot +from common import const from common.log import logger from channel.channel import Channel -BOT_TOKEN = '5684663516:AAFRYGIcDalEZzExyoBwt-t33BZjxcTISxg' -bot = telebot.TeleBot(BOT_TOKEN) +from config import channel_conf_val, channel_conf +bot = telebot.TeleBot(token=channel_conf(const.TELEGRAM).get('bot_token')) thread_pool = ThreadPoolExecutor(max_workers=8) -@bot.message_handler(commands=['ask']) +@bot.message_handler(content_types=['text']) def send_welcome(msg): + # telegram消息处理 TelegramChannel().handle(msg) class TelegramChannel(Channel): @@ -22,10 +24,45 @@ class TelegramChannel(Channel): bot.infinity_polling() def handle(self, msg): logger.debug("[Telegram]receive msg: " + msg.text) - thread_pool.submit(self._dosend,msg.text.replace("/ask",""),msg) + img_match_prefix = self.check_prefix(msg, channel_conf_val(const.TELEGRAM, 'image_create_prefix')) + if img_match_prefix: + thread_pool.submit(self._do_send_img, msg, str(msg.chat.id)) + else: + thread_pool.submit(self._dosend,msg.text,msg) + def _dosend(self,query,msg): context= dict() - context['from_user_id'] = '1111' + context['from_user_id'] = str(msg.chat.id) reply_text = super().build_reply_content(query, context) logger.info('[Telegram]] reply content: {}'.format(reply_text)) - bot.reply_to(msg,reply_text) \ No newline at end of file + bot.reply_to(msg,reply_text) + + def _do_send_img(self, msg, reply_user_id): + try: + if not msg: + return + context = dict() + context['type'] = 'IMAGE_CREATE' + img_url = super().build_reply_content(msg.text, context) + if not img_url: + return + + # 图片下载 + pic_res = requests.get(img_url, stream=True) + image_storage = io.BytesIO() + for block in pic_res.iter_content(1024): + image_storage.write(block) + image_storage.seek(0) + + # 图片发送 + logger.info('[Telegrame] sendImage, receiver={}'.format(reply_user_id)) + bot.reply_to(msg, image_storage) + except Exception as e: + logger.exception(e) + + + def check_prefix(self,msg,prefix_list): + for prefix in prefix_list: + if msg.text.startswith(prefix): + return prefix + return None \ No newline at end of file diff --git a/config-template.json b/config-template.json index 0a3d14f..aec1cd3 100644 --- a/config-template.json +++ b/config-template.json @@ -30,6 +30,9 @@ "subject_keyword": ["bot", "@bot"], "host_email": "xxxx@gmail.com", "host_password": "GMAIL ACCESS KEY" + }, + "telegram":{ + "bot_token": "5684663516:AAFRYGIcDalEZzExyoBwt-t33BZjxcTISxg" } } }