写点什么

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

作者:Hanson
  • 2022 年 9 月 01 日
    广东
  • 本文字数:688 字

    阅读完需:约 2 分钟

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

项目链接

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

使用

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


CALLBACK_URL=http://127.0.0.1:5678SECRET=xxxPORT=5557
复制代码


clone 代码


git clone https://github.com/xbot-org/demo_keyword_reply.gitcd demo_keyword_reply
复制代码


运行此服务 php -S localhost:5678

效果图

代码

代码很简单,这里贴一下


<?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

用户头像

Hanson

关注

还未添加个人签名 2019.07.12 加入

还未添加个人简介

评论

发布
暂无评论
基于 xbot 实现微信关键词自动回复_微信_Hanson_InfoQ写作社区