V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
爱意满满的作品展示区。
HanSonJ
V2EX  ›  分享创造

基于 xbot 实现微信关键词自动回复

  •  
  •   HanSonJ ·
    HanSon · 2022-09-01 17:22:23 +08:00 · 1988 次点击
    这是一个创建于 595 天前的主题,其中的信息可能已经有所发展或是发生改变。

    基于 xbot 实现微信关键词自动回复

    项目链接

    https://github.com/xbot-org/demo_keyword_reply

    使用

    先配置好 xbot 的配置如下,并运行

    CALLBACK_URL=http://127.0.0.1:5678
    SECRET=xxx
    PORT=5557
    

    clone 代码

    git clone https://github.com/xbot-org/demo_keyword_reply.git
    cd demo_keyword_reply
    

    运行此服务 php -S localhost:5678

    效果图

    1662022320_1_.png

    代码

    代码很简单,这里贴一下

    <?php
    
    $jsonString = file_get_contents("php://input");
    
    $msg = json_decode($jsonString, true);
    
    switch ($msg['type']) {
        case 'MT_RECV_TEXT_MSG':
            if ($msg['data']['msg'] == 'ping') {
                sendText($msg['data']['from_wxid'], 'pong');
            } elseif ($msg['data']['msg'] == '123') {
                sendText($msg['data']['from_wxid'], '456');
            }
    }
    
    echo '{}';
    
    function sendText($wxid, $content) {
        post(json_encode([
            'client_id' => 1,
            'is_sync' => 1,
            'data' => [
                'to_wxid' => $wxid,
                'content' => $content,
            ],
            'type' => 'MT_SEND_TEXTMSG',
        ]));
    }
    
    function post($data) {
        $opts = array('http' =>
            array(
                'method'  => 'POST',
                'header'  => 'Content-Type: application/json',
                'content' => $data
            )
        );
    
        $context  = stream_context_create($opts);
    
        file_get_contents('http://127.0.0.1:5557', false, $context);
    }
    

    xbot 文档

    https://www.apifox.cn/apidoc/shared-71b9855b-693c-48fc-858b-cde2c5afe5a8

    8 条回复    2022-09-04 02:16:53 +08:00
    prudence
        1
    prudence  
       2022-09-01 18:09:12 +08:00
    这种都不敢上大号,谁知道会不会泄露什么数据信息,当然我也没啥见不得人
    HanSonJ
        2
    HanSonJ  
    OP
       2022-09-01 18:11:38 +08:00
    @prudence #1 不是登录云服务的,是在你自己电脑上使用的
    Yzh361
        3
    Yzh361  
       2022-09-01 21:43:31 +08:00 via Android
    我还实现了机器人自动对话…
    yhtbiy
        4
    yhtbiy  
       2022-09-02 09:38:51 +08:00
    直接说怎么卖
    avenger
        5
    avenger  
       2022-09-02 12:24:38 +08:00
    vbot 都有 api 了,赞
    chenzhe
        6
    chenzhe  
       2022-09-02 14:07:01 +08:00 via iPhone
    @avenger 活捉一只也成陈老板
    codingBug
        7
    codingBug  
       2022-09-02 22:17:48 +08:00
    不开源不敢用啊
    mumu
        8
    mumu  
       2022-09-04 02:16:53 +08:00
    看着是广告啊
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1245 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 21ms · UTC 17:50 · PVG 01:50 · LAX 10:50 · JFK 13:50
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.