Add func_tigerbot
This commit is contained in:
38
func_tigerbot.py
Normal file
38
func_tigerbot.py
Normal 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)
|
||||
Reference in New Issue
Block a user