C++ Header-only 企业微信消息推送服务器
- 下面是用wxserver接入的ChatGPT
- 详细搭建过程
- 下面是一个消息回复示例
ws::Server server;
server.load_config("config.czh");
server.add_msg_handle(
[&server](const ws::Request &req, ws::Message &res)
{
res.set_content(ws::MsgType::text, "hello, world");
});
server.run();添加当接受的消息时的回调函数
[&server](const ws::Request &req, ws::Message &res){}user_id消息发送者的idcontent消息内容。
指定回复的用户,默认为消息发送者
指定消息回复内容
| MsgType | info |
|---|---|
| text | 文本 |
| markdown | markdown子集 |
| image | 图片(path) |
| file | 文件(path) |
server.add_msg_handle(
[&server](const ws::Request &req, ws::Message &res)
{
std::thread(
[req, &server]()
{
//do something
res.set_content(ws::MsgType::text, ret);
}).detach();
});server.send_message({ws::MsgType::text, "hello", "caozhanhao"});
server.send_message({ws::MsgType::file, "LICENSE", "caozhanhao"});
server.send_message({ws::MsgType::image, "example.jpg", "caozhanhao"});
server.send_message({ws::MsgType::markdown, "`hello world`", "caozhanhao"});
// ... 同set_contentServer(std::string host, int port_, int agent_id, const std::string& token, const std::string encoding_aes_key,
const std::string corp_id, const std::string corp_secret,
bool enable_console_logger, const std::string& logging_path = "")ws::Server server;
server.load_config("config.czh");
// or
// auto config = ws::parse_config("config.czh");
// server.load_config(config);| server | info |
|---|---|
| host | 运行地址 |
| port | 运行端口 |
| enable_console_logger | 日志控制台输出, false不输出 |
| logging_path | 日志文件, null不输出到文件 |
| weixin | info |
|---|---|
| AgentId | 位于我的企业/xxx/AgentId |
| CorpID | 位于我的企业/企业信息/企业ID |
| CorpSecret | 位于应用管理/xxx/Secret |
| Token和EncodingAESKey | 位于应用管理/xxx/功能/设置API接收/ |
注:以下为示例中需要的配置文件,不是wxserver必须的
| hugging_face | info |
|---|---|
| model | 需Conversational,如ingen51/DialoGPT-medium-GPT4 |
| token | HuggingFace token |
| openai | info |
|---|---|
| model | 如gpt-3.5-turbo |
| token | OpenAI token |
| proxy | Http代理地址 |
| proxy_port | Http代理端口 |
g++ examples/src/main.cpp -I examples/src -I include -I thirdparty -I thirdparty/json/include -I thirdparty/libczh/include -lssl -lcrypto -lpthread -O2 -std=c++2a -o wxserver-linux- 注意将OpenSSL的目录替换为你自己的。
g++ examples/src/main.cpp -I examples/src -I "C:\Program Files\OpenSSL-Win64\include" -I include -I thirdparty -I thirdparty/json/include -I thirdparty/libczh/include -L "C:\Program Files\OpenSSL-Win64\lib" -lssl -lcrypto -l ws2_32 -l crypt32 -lpthread -O2 -std=c++2a -o wxserver-windows.exe- 发送文件时,文件须小于20 MB
- openssl
- nlohmann/json
- cpp-httplib
- libczh
- 需C++ 20
- 欢迎PR

