diff --git a/func_tigerbot.py b/func_tigerbot.py new file mode 100644 index 0000000..b47a797 --- /dev/null +++ b/func_tigerbot.py @@ -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)