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

2
http/MANIFEST.in Normal file
View File

@ -0,0 +1,2 @@
include wcferry/*.dll
include wcferry/*.exe

26
http/README.MD Normal file
View File

@ -0,0 +1,26 @@
# WeChatFerry HTTP 客户端
基于 [wcferry](https://pypi.org/project/wcferry/) 封装。
## 快速开始
### 安装
```sh
pip install --upgrade wcfhttp
```
### 运行
```sh
# 查看版本
wcfhttp -v
# 查看帮助
wcfhttp -h
# 忽略新消息运行
wcfhttp
# 新消息转发到指定地址
wcfhttp --cb http://your_host:your_port/callback
```
### 接收消息回调接口文档
参考文档默认地址为http://localhost:9999/docs`接收消息回调样例`。

50
http/setup.py Normal file
View File

@ -0,0 +1,50 @@
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
from __future__ import print_function
from setuptools import setup, find_packages
import wcfhttp
with open("README.md", "r", encoding="utf-8") as fh:
long_description = fh.read()
setup(
name="wcfhttp",
version=wcfhttp.core.__version__,
author="Changhua",
author_email="lichanghua0821@gmail.com",
description="一个玩微信的工具",
long_description=long_description,
long_description_content_type="text/markdown",
license="MIT",
url="https://github.com/lich0821/WeChatFerry",
python_requires=">=3.8",
packages=find_packages(),
include_package_data=True,
entry_points={
'console_scripts': [
'wcfhttp=wcfhttp.main:main'
]
},
install_requires=[
"setuptools",
f"wcferry=={wcfhttp.core.__version__}",
"fastapi",
"uvicorn[standard]"
],
classifiers=[
"Environment :: Win32 (MS Windows)",
"Intended Audience :: Developers",
"Intended Audience :: Customer Service",
"Topic :: Communications :: Chat",
"Operating System :: Microsoft :: Windows",
"Programming Language :: Python",
],
project_urls={
"Documentation": "https://wechatferry.readthedocs.io/zh/latest/index.html",
"GitHub": "https://github.com/lich0821/WeChatFerry/",
},
)

3
http/wcfhttp/__init__.py Normal file
View File

@ -0,0 +1,3 @@
# -*- coding: utf-8 -*-
from wcfhttp.core import Http, __version__

69
http/wcfhttp/core.py Normal file
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
http/wcfhttp/main.py Normal file
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()