Add func_tigerbot

This commit is contained in:
Changhua
2023-07-07 07:31:24 +08:00
parent 49faac4f23
commit 0ae9370df6

38
func_tigerbot.py Normal file
View File

@@ -0,0 +1,38 @@
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
import logging
import requests
from random import randint
class TigerBot():
def __init__(self, tbconf=None) -> None:
self.LOG = logging.getLogger(__file__)
self.tburl = "https://api.tigerbot.com/bot-service/ai_service/gpt"
self.tbheaders = {"Authorization": "Bearer " + tbconf["key"]}
self.tbmodel = tbconf["model"]
self.fallback = ["", "快滚", "赶紧滚"]
def answer(self, msg: str, sender: str = None) -> str:
payload = {
"text": msg,
"modelVersion": self.tbmodel
}
try:
rsp = requests.post(self.tburl, headers=self.tbheaders, json=payload).json()["data"]["result"][0]
except Exception as e:
self.LOG.error(f"{e}")
idx = randint(0, len(self.fallback))
rsp = self.fallback[idx]
return rsp
if __name__ == "__main__":
from configuration import Config
c = Config()
tbot = TigerBot(c.TIGERBOT)
rsp = tbot.answer("你还活着?")
print(rsp)