Add tigerbot

This commit is contained in:
Changhua
2023-07-09 07:57:39 +08:00
parent 78fa5035a1
commit bfb1735dd3
2 changed files with 16 additions and 9 deletions

View File

@@ -15,16 +15,18 @@ class TigerBot():
self.tbmodel = tbconf["model"] self.tbmodel = tbconf["model"]
self.fallback = ["", "快滚", "赶紧滚"] self.fallback = ["", "快滚", "赶紧滚"]
def answer(self, msg: str, sender: str = None) -> str: def get_answer(self, msg: str, sender: str = None) -> str:
payload = { payload = {
"text": msg, "text": msg,
"modelVersion": self.tbmodel "modelVersion": self.tbmodel
} }
rsp = ""
try: try:
rsp = requests.post(self.tburl, headers=self.tbheaders, json=payload).json()["data"]["result"][0] rsp = requests.post(self.tburl, headers=self.tbheaders, json=payload).json()
rsp = rsp["data"]["result"][0]
except Exception as e: except Exception as e:
self.LOG.error(f"{e}") self.LOG.error(f"{e}: {payload}\n{rsp}")
idx = randint(0, len(self.fallback)) idx = randint(0, len(self.fallback) - 1)
rsp = self.fallback[idx] rsp = self.fallback[idx]
return rsp return rsp
@@ -34,5 +36,5 @@ if __name__ == "__main__":
from configuration import Config from configuration import Config
c = Config() c = Config()
tbot = TigerBot(c.TIGERBOT) tbot = TigerBot(c.TIGERBOT)
rsp = tbot.answer("你还活着?") rsp = tbot.get_answer("你还活着?")
print(rsp) print(rsp)

View File

@@ -11,6 +11,7 @@ from configuration import Config
from func_chatgpt import ChatGPT from func_chatgpt import ChatGPT
from func_chengyu import cy from func_chengyu import cy
from func_news import News from func_news import News
from func_tigerbot import TigerBot
from job_mgmt import Job from job_mgmt import Job
@@ -24,10 +25,14 @@ class Robot(Job):
self.LOG = logging.getLogger("Robot") self.LOG = logging.getLogger("Robot")
self.wxid = self.wcf.get_self_wxid() self.wxid = self.wcf.get_self_wxid()
self.allContacts = self.getAllContacts() self.allContacts = self.getAllContacts()
self.chat = None
chatgpt = self.config.CHATGPT if self.config.TIGERBOT:
if chatgpt: self.chat = TigerBot(self.config.TIGERBOT)
self.chat = ChatGPT(chatgpt.get("key"), chatgpt.get("api"), chatgpt.get("proxy"), chatgpt.get("prompt")) elif self.config.CHATGPT:
cgpt = self.config.CHATGPT
self.chat = ChatGPT(cgpt.get("key"), cgpt.get("api"), cgpt.get("proxy"), cgpt.get("prompt"))
else:
self.chat = None
def toAt(self, msg: WxMsg) -> bool: def toAt(self, msg: WxMsg) -> bool:
"""处理被 @ 消息 """处理被 @ 消息