当前位置: 首页 > 工具软件 > EasyChat > 使用案例 >

小程序转发客服消息使用easychat

曹臻
2023-12-01

需求:

收到小程序卡片时,发送公众号二维码让其绑定,收到其他信息转发到网页客服系统

实现

收到消息,响应时回复一段xml如下,

<xml>
    <ToUserName><![CDATA[touser]]></ToUserName>
    <FromUserName><![CDATA[fromuser]]></FromUserName>
    <CreateTime>1399197672</CreateTime>
    <MsgType><![CDATA[transfer_customer_service]]></MsgType>
</xml>

ps

1其中 FromUserName 为小程序原始id,

2经实际测试消息推送配置里数据格式选xml才可以

代码

<?php

namespace app\api\controller;
use app\admin\model\Setting;
use EasyWeChat\Factory;
use think\Controller;
use think\facade\Env;
use think\facade\Log;
use think\Request;
use message\MessageHandler;
use EasyWeChat\Kernel\Messages\Text;
use unit\Wechat;

/**微信消息处理
 * Class Message
 * @package app\api\controller
 */
class Message extends Controller
{
    private $messageApp;
    public function __construct()
    {

        $wechat = new Wechat();
        $this->messageApp = $wechat->app;
        //微信首次接入验证
        if (!empty($_GET['echostr']) && $wechat->checkSignature()) {
            header('content-type:text');
            echo $_GET['echostr'];
            exit;
        }

    }


    public function index(Request $request)
    {
        $this->messageApp->server->push(function ($message) {

//
            $handler = new MessageHandler($message);
            switch ($message['MsgType']) {
                case 'miniprogrampage':
                    return $handler->miniProgramPageEvent();
                    break;
                default:
                    return $handler->transferEvent();
                    break;
            }
        });

        $response = $this->messageApp->server->serve();
        $response->send();
        //return $response;

    }
   
}


/**转发客服消息
     * @return Raw
     */
    public function transferEvent()
    {
        $openid = $this->message['FromUserName'];
        $appId = $this->message['ToUserName'];
        $createTine = $this->message['CreateTime'];
        $xml = "<xml><ToUserName><![CDATA[$openid]]></ToUserName><FromUserName><![CDATA[$appId]]></FromUserName><CreateTime>$createTine</CreateTime><MsgType><![CDATA[transfer_customer_service]]></MsgType></xml>";
        $message = new Raw($xml);
        Log::write('mm:' . json_encode($message));
        return $message;
    }

 类似资料: