RsCode.WeChat中,接收到微信推送的消息,是通过IWeChatEventHandler类来处理的

收到微信消息后自定义逻辑

如果需要自定义收到微信消息的逻辑,可自定义类,并继承WeChatEventHandler,具体使用示例如下:

  1. 自定义消息处理类CustomWxMsgHandler,继承WeChatEventHandler

  2. 添加服务

services.AddScoped<IWeChatEventHandler, CustomWxMsgHandler>(); 
  1. 示例代码

    public class CustomWxMsgHandler:WeChatEventHandler
    {
            //点击菜单要返回的事件,向用户发消息时,返回消息体加密后的字符串
         public override async Task<string> OnMenuClickEvent(MenuClickEventMessage menuClickEvent)
         {
             log.LogInformation("OnMenuClickEvent=" + JsonSerializer.Serialize(menuClickEvent));
            if (menuClickEvent.EventKey == "散财童子")
            {
                var fromUserName = menuClickEvent.FromUserName;
                var toUserName = menuClickEvent.ToUserName;
                TextMessage msg = new TextMessage(fromUserName, toUserName, "欢迎关注软商网络");
                var options = WeChatOptions.FirstOrDefault(c => c.Id == toUserName);
                wechat.UseAppId(options.AppId);
                var d = await wechat.SendMessageAsync<TextMessage>(msg);
                return d;
            }
            else
                return "success";
        }
        //微信后台推送的component_ticket
        public override async Task<string> OnComponentVerifyTicketEvent(ThirdPlatformMessage data)
        {
            var key = "component_verify_ticket";
            await cache.SetAsync(key,data,DateTime.Now.AddHours(11));//保存11个小时
            return "success";
        }
    }
    

可自定义的消息与事件

#region 普通消息
    /// <summary>
    /// 收到微信推送的图片消息时的处理
    /// </summary>
    /// <param name="receiveMessage"></param>
    /// <returns></returns>
    Task<string> OnReceiveImageMessageEvent(ImageMessage receiveMessage);
    /// <summary>
    /// 收到微信推送的文本消息时时的处理
    /// </summary>
    /// <param name="receiveMessage"></param>
    /// <returns>返回</returns>
    Task<string> OnReceiveTextMessageEvent(TextMessage receiveMessage);
    /// <summary>
    /// 收到微信推送的链接时的处理
    /// </summary>
    /// <param name="receiveMessage"></param>
    /// <returns></returns>
    Task<string> OnReceiveLinkMessageEvent(LinkMessage receiveMessage);
    /// <summary>
    /// 收到微信推送的短视频消息
    /// </summary>
    /// <param name="receiveMessage"></param>
    /// <returns></returns>
    Task<string> OnReceiveShortVideoMessageEvent(ShortVideoMessage receiveMessage);

    /// <summary>
    /// 收到微信推送的视频消息
    /// </summary>
    /// <param name="receiveMessage"></param>
    /// <returns></returns>
    Task<string> OnReceiveVideoMessageEvent(VideoMessage receiveMessage);
    /// <summary>
    /// 收到微信推送的语音消息
    /// </summary>
    /// <param name="receiveMessage"></param>
    /// <returns></returns>
    Task<string> OnReceiveVoiceMessageEvent(VoiceMessage receiveMessage);
    /// <summary>
    /// 收到微信推送的地理位置消息
    /// </summary>
    /// <param name="receiveMessage"></param>
    /// <returns></returns>
    Task<string> OnReceiveLocationMessageEvent(LocationMessage receiveMessage);
    /// <summary>
    /// 转发微信公众号客服消息
    /// </summary>
    /// <param name="customerServiceMessage"></param>
    /// <returns></returns>
    Task<string> OnTransferCustomerServiceMessageEvent(TransferCustomerServiceMessage customerServiceMessage);
#endregion
    #region 事件推送
    /// <summary>
    /// 关注公众号时
    /// </summary>
    /// <param name="subscribeEventMessage">收到的信息</param>
    /// <returns></returns>
    Task<string> OnSubscribeEvent(SubscribeEventMessage subscribeEventMessage);

    /// <summary>
    /// 取消订阅时
    /// </summary>
    /// <param name="subscribeEventMessage"></param>
    /// <returns></returns>
    Task<string> OnUnSubscribeEvent(UnSubscribeEventMessage subscribeEventMessage);

    /// <summary>
    /// 用户已关注时的扫描带参数的二维码
    /// </summary>
    /// <param name="scanEventMessage"></param>
    /// <returns></returns>
    Task<string> OnCanEvent(ScanEventMessage scanEventMessage);
    /// <summary>
    /// 上报地理位置事件
    /// </summary>
    /// <param name="locationEventMessage"></param>
    /// <returns></returns>
    Task<string> OnLocationEvent(LocationEventMessage locationEventMessage);
    /// <summary>
    /// 点击菜单拉取消息时的事件推送  
    /// </summary>
    /// <param name="menuClickEvent"></param>
    /// <returns></returns>
    Task<string> OnMenuClickEvent(MenuClickEventMessage menuClickEvent);
    /// <summary>
    /// 点击菜单跳转链接时的事件推送 
    /// </summary>
    /// <param name="menuViewEvent"></param>
    /// <returns></returns>
    Task<string> OnMenuViewEvent(MenuViewEventMessage menuViewEvent);

    /// <summary>
    /// 用户进入客服会话
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    Task<string> OnUserEnterTempSessionEvent(MessageBase data);

    /// <summary>
    /// 用户自定义事件
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    Task<string> OnCustomEvent(MessageBase data);

    /// <summary>
    /// 第三方平台收到的ticket
    /// </summary>
    /// <param name="weChatRequestData"></param>
    /// <returns></returns>
    Task<string> OnComponentVerifyTicketEvent(ThirdPlatformMessage data);
#endregion

消息类型

TextMessage文字消息
ImageMessage 图片消息
LinkMessage 链接消息
LocationMessage位置消息
ShortVideoMessage短视频消息
TextImageMessage图文消息
VideoMessage视频消息
VoiceMessage语音消息

ThirdPlatformMessage收到到component_ticket值

上次更新:
贡献者: kuiyu