Add http client

This commit is contained in:
Changhua
2023-05-06 20:53:26 +08:00
parent 4a2583721c
commit 951f5241fe
6 changed files with 190 additions and 0 deletions
+3
View File
@@ -0,0 +1,3 @@
# -*- coding: utf-8 -*-
from wcfhttp.core import Http, __version__
+69
View File
@@ -0,0 +1,69 @@
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
import logging
from typing import Any
import requests
from fastapi import FastAPI
from pydantic import BaseModel
from wcferry import Wcf, WxMsg
__version__ = "3.7.0.30.25"
class Msg(BaseModel):
id: str
type: int
xml: str
sender: str
roomid: str
content: str
thumb: str
extra: str
is_self: bool
is_group: bool
class Http(FastAPI):
"""WeChatFerry HTTP 客户端,文档地址:http://IP:PORT/docs"""
def __init__(self, wcf: Wcf, cb: str, **extra: Any) -> None:
super().__init__(**extra)
self.LOG = logging.getLogger(__name__)
self.wcf = wcf
self._set_cb(cb)
self.add_api_route("/msg_cb", self.msg_cb, methods=["POST"], summary="接收消息回调样例")
def _set_cb(self, cb):
def callback(msg: WxMsg):
data = {}
data["id"] = msg.id
data["type"] = msg.type
data["xml"] = msg.xml
data["sender"] = msg.sender
data["roomid"] = msg.roomid
data["content"] = msg.content
data["thumb"] = msg.thumb
data["extra"] = msg.extra
data["is_self"] = msg.from_self()
data["is_group"] = msg.from_group()
try:
rsp = requests.post(url=cb, json=data)
if rsp.status_code != 200:
self.LOG.error(f"消息转发失败,HTTP 状态码为: {rsp.status_code}")
except Exception as e:
self.LOG.error(f"消息转发异常: {e}")
if cb:
self.LOG.info(f"消息回调: {cb}")
self.wcf.enable_recv_msg(callback=callback)
else:
self.LOG.info(f"没有设置回调,打印消息")
self.wcf.enable_recv_msg(print)
def msg_cb(self, msg: Msg):
"""示例回调方法,简单打印消息"""
print(f"收到消息:{msg}")
return {"status": 0, "message": "成功"}
+40
View File
@@ -0,0 +1,40 @@
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
import logging
import argparse
import uvicorn
from wcferry import Wcf
from wcfhttp import Http, __version__
def main():
parse = argparse.ArgumentParser()
parse.add_argument("-v", "--version", action="version", version=f"{__version__}")
parse.add_argument("--wcf_host", type=str, default=None, help="WeChatFerry 监听地址,默认本地启动监听 0.0.0.0")
parse.add_argument("--wcf_port", type=int, default=10086, help="WeChatFerry 监听端口 (同时占用 port + 1 端口),默认 10086")
parse.add_argument("--wcf_debug", type=bool, default=False, help="是否打开 WeChatFerry 调试开关")
parse.add_argument("--host", type=str, default="0.0.0.0", help="wcfhttp 监听地址,默认监听 0.0.0.0")
parse.add_argument("--port", type=int, default=9999, help="wcfhttp 监听端口,默认 9999")
parse.add_argument("--cb", type=str, default="", help="接收消息回调地址")
logging.basicConfig(level="INFO", format="%(asctime)s %(message)s")
args = parse.parse_args()
cb = args.cb
if not cb:
logging.warning("没有设置接收消息回调,消息直接通过日志打印;请通过 --cb 设置消息回调")
logging.warning(f"回调接口规范参考接收消息回调样例:http://{args.host}:{args.port}/docs")
wcf = Wcf(args.wcf_host, args.wcf_port, args.wcf_debug)
home = "https://github.com/lich0821/WeChatFerry"
http = Http(wcf=wcf,
cb=cb,
title="WeChatFerry HTTP 客户端",
description=f"Github: <a href='{home}'>WeChatFerry</a>",)
uvicorn.run(app=http, host=args.host, port=args.port)
if __name__ == "__main__":
main()