diff --git a/config.yaml.template b/config.yaml.template
index 508c111..f4688ff 100644
--- a/config.yaml.template
+++ b/config.yaml.template
@@ -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
diff --git a/configuration.py b/configuration.py
index 8eacc21..8b6ccf4 100644
--- a/configuration.py
+++ b/configuration.py
@@ -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")
diff --git a/func_http.py b/func_http.py
new file mode 100644
index 0000000..d4b35ad
--- /dev/null
+++ b/func_http.py
@@ -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: WeChatFerry",)
+ Http.start(http, c.get("host", "0.0.0.0"), c.get("port", 9999))
+
+ while True:
+ time.sleep(1)
diff --git a/main.py b/main.py
index a29cede..2e45257 100644
--- a/main.py
+++ b/main.py
@@ -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()
diff --git a/requirements.txt b/requirements.txt
index 80190aa..5deb5ba 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,5 +1,7 @@
+fastapi
openai
pandas
pyyaml
schedule
+uvicorn[standard]
wcferry
diff --git a/robot.py b/robot.py
index b287577..e197e9e 100644
--- a/robot.py
+++ b/robot.py
@@ -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: WeChatFerry",)
+ Http.start(http, c["host"], c["port"])