Impl HTTP API

This commit is contained in:
Changhua
2023-04-03 12:24:18 +08:00
parent e96f3800e8
commit 891cada955
6 changed files with 86 additions and 2 deletions

View File

@@ -46,3 +46,8 @@ groups:
# key: 填写你 ChatGPT 的 key
# api: https://api.openai.com/v1 # 如果你不知道这是干嘛的,就不要改
# proxy: # 如果你在国内你可能需要魔法大概长这样http://域名或者IP地址:端口号
# 如果需要暴露 HTTP 接口,取消下面的注释
# http:
# host: 0.0.0.0
# port: 9999

View File

@@ -29,3 +29,4 @@ class Config(object):
logging.config.dictConfig(yconfig["logging"])
self.GROUPS = yconfig["groups"]["enable"]
self.CHATGPT = yconfig.get("chatgpt")
self.HTTP = yconfig.get("http")

55
func_http.py Normal file
View File

@@ -0,0 +1,55 @@
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
import logging
from typing import Any
import uvicorn
import threading
from fastapi import FastAPI
from wcferry import Wcf
class Http(FastAPI):
"""将 wcferry 能力转成 HTTP 协议的示例"""
def __init__(self, wcf: Wcf, **extra: Any) -> None:
super().__init__(**extra)
self.wcf = wcf
self.LOG = logging.getLogger(__name__)
self.add_api_route("/send", self.send_text, methods=["GET"])
def send_text(self, msg: str, receiver: str, aters: str = "") -> dict:
ret = self.wcf.send_text(msg, receiver, aters)
return {"status": ret, "msg": msg, "receiver": receiver, "aters": aters}
@staticmethod
def start(http, host, port):
threading.Thread(name="HTTP",
target=uvicorn.run,
kwargs={"app": http, "host": host, "port": port}).start()
if __name__ == "__main__":
import time
import signal
from configuration import Config
c = Config().HTTP
if not c:
exit(0)
def handler(sig, frame):
exit(0)
signal.signal(signal.SIGINT, handler)
wcf = Wcf("tcp://127.0.0.1:10086")
home = "https://github.com/lich0821/WeChatFerry"
http = Http(wcf=wcf,
title="API for send text",
description=f"Github: <a href='{home}'>WeChatFerry</a>",)
Http.start(http, c.get("host", "0.0.0.0"), c.get("port", 9999))
while True:
time.sleep(1)

View File

@@ -37,6 +37,13 @@ def main():
# 机器人启动发送测试消息
robot.sendTextMsg("机器人启动成功!", "filehelper")
# 暴露 HTTP 接口供发送消息,需要在配置文件中取消 http 注释
# 接口文档http://localhost:9999/docs
# 访问示例:
# 1. 浏览器访问http://localhost:9999/send?msg=hello%20world&receiver=filehelper
# 2. curl -X 'GET' 'http://localhost:9999/send?msg=hello%20world&receiver=filehelper' -H 'accept: application/json'
robot.enableHTTP()
# 接收消息
robot.enableRecvMsg()

View File

@@ -1,5 +1,7 @@
fastapi
openai
pandas
pyyaml
schedule
uvicorn[standard]
wcferry

View File

@@ -1,15 +1,17 @@
# -*- coding: utf-8 -*-
import logging
import re
import time
import logging
import xml.etree.ElementTree as ET
import uvicorn
from wcferry import Wcf
from configuration import Config
from func_chengyu import cy
from func_chatgpt import ChatGPT
from func_chengyu import cy
from func_http import Http
from job_mgmt import Job
@@ -186,3 +188,15 @@ class Robot(Job):
# 添加了好友,更新好友列表
self.allContacts[msg.sender] = nickName[0]
self.sendTextMsg(f"Hi {nickName[0]},我自动通过了你的好友请求。", msg.sender)
def enableHTTP(self) -> None:
"""暴露 HTTP 发送消息接口供外部调用,不配置则忽略"""
c = self.config.HTTP
if not c:
return
home = "https://github.com/lich0821/WeChatFerry"
http = Http(wcf=self.wcf,
title="API for send text",
description=f"Github: <a href='{home}'>WeChatFerry</a>",)
Http.start(http, c["host"], c["port"])