diff --git a/channel/channel_factory.py b/channel/channel_factory.py index 693aae1..d900276 100644 --- a/channel/channel_factory.py +++ b/channel/channel_factory.py @@ -28,6 +28,9 @@ def create_channel(channel_type): elif channel_type == const.GMAIL: from channel.gmail.gmail_channel import GmailChannel return GmailChannel() + elif channel_type == const.TELEGRAM: + from channel.telegram.telegram_channel import TelegramChannel + return TelegramChannel() else: raise RuntimeError diff --git a/channel/telegram/telegram_channel.py b/channel/telegram/telegram_channel.py new file mode 100644 index 0000000..c989c2c --- /dev/null +++ b/channel/telegram/telegram_channel.py @@ -0,0 +1,30 @@ +import json +from concurrent.futures import ThreadPoolExecutor +import io +import requests +import telebot +from common.log import logger +from channel.channel import Channel +BOT_TOKEN = '5684663516:AAFRYGIcDalEZzExyoBwt-t33BZjxcTISxg' +bot = telebot.TeleBot(BOT_TOKEN) +thread_pool = ThreadPoolExecutor(max_workers=8) + + +@bot.message_handler(commands=['ask']) +def send_welcome(msg): + TelegramChannel().handle(msg) + +class TelegramChannel(Channel): + def __init__(self): + pass + def startup(self): + logger.info("开始启动telegram机器人") + def handle(self, msg): + logger.debug("[Telegram]receive msg: " + msg.text) + thread_pool.submit(self._dosend,msg.text.replace("/ask",""),msg) + def _dosend(self,query,msg): + context= dict() + context['from_user_id'] = '1111' + 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 diff --git a/common/const.py b/common/const.py index 17ecc4d..b403c28 100644 --- a/common/const.py +++ b/common/const.py @@ -4,6 +4,7 @@ WECHAT = "wechat" WECHAT_MP = "wechat_mp" WECHAT_MP_SERVICE = "wechat_mp_service" GMAIL = "gmail" +TELEGRAM = "telegram" # model OPEN_AI = "openai" diff --git a/config-template.json b/config-template.json index b601557..926784d 100644 --- a/config-template.json +++ b/config-template.json @@ -2,13 +2,13 @@ "model": { "type" : "openai", "openai": { - "api_key": "YOUR API KEY", - "conversation_max_tokens": 1000, + "api_key": "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", + "conversation_max_tokens": 2000, "character_desc": "你是ChatGPT, 一个由OpenAI训练的大型语言模型, 你旨在回答并解决人们的任何问题,并且可以使用多种语言与人交流。" } }, "channel": { - "type": "terminal", + "type": "telegram", "single_chat_prefix": ["bot", "@bot"], "single_chat_reply_prefix": "[bot] ", "group_chat_prefix": ["@bot"],