Skip to content

v1.1 新版消息处理方式

Dong chen edited this page Sep 5, 2013 · 1 revision

首先定义消息处理逻辑(别看代码多,其实很简单,实现一个接口就行了)

/*******************************
 *	Author:	Dong [mailto:[email protected]] 欢迎交流 Q群:289147891
 *	Date:	2013-09-05 22:18:33
 *	Desc:	
 * 
*******************************/

using System.Collections.Generic;
using Td.Weixin.Public.Message;

/// <summary>
/// 消息处理
/// </summary>
public class WxMsgHandler : IMessageHandler
{
    public ResponseMessage OnTextMessage(RecTextMessage msg)
    {
        //文本响应
        var r = msg.GetTextResponse();
        r.Data = (TextMsgData)"this is response";

        //根据内容决定响应。此处为模拟
        switch (msg.Content)
        {
            case "image":
                return SimulateMusicMessage();

            case "news":
                return SimulateNewsMessage();
            
            case "event":
                r.Data = (TextMsgData) "恭喜你,你点中了菜单,要中奖了";
                break;

        }
        return r;
    }

    public ResponseMessage OnImageMessage(RecImageMessage msg)
    {
        var r = SimulateMusicMessage();
        return r;
    }

    public ResponseMessage OnLinkMessage(RecLinkMessage msg)
    {
        var r = msg.GetNewsResponse();
        return r;
    }


    public ResponseMessage OnEventMessage(RecEventMessage msg)
    {
        var rep = msg.GetTextResponse();
        return rep;
    }

    public ResponseMessage OnLocationMessage(RecLocationMessage msg)
    {
        var r = SimulateNewsMessage();
        return r;
    }

    /********************   下面的代码仅仅是测试用了,实际并不需要 ********************/

    private static RepMusicMessage SimulateMusicMessage()
    {
        //音乐响应
        var r = new RepMusicMessage();
        r.Data = new MusicMsgData
        {
            Description = "desc",
            Title = "title",
            MusicUrl =
                "http://play.baidu.com/?__m=mboxCtrl.playSong&__a=331525&__o=/song/331525||playBtn&fr=ps||www.baidu.com#loaded",
            HQMusicUrl = "hq url"
        };
        return r;
    }

    private static RepNewsMessage SimulateNewsMessage()
    {
        //图文响应
        var r = new RepNewsMessage();
        r.Data = new NewsMsgData
        {
            //响应3条新闻信息
            Items = new List<NewsItem>
            {
                new NewsItem
                {
                    Description = "new1",
                    Title = "老三发了",
                    PicUrl = "http://c.hiphotos.baidu.com/ting/pic/item/b8014a90f603738d538032bfb21bb051f919ec61.jpg",
                    Url = "http://baidu.com"
                },
                new NewsItem
                {
                    Description = "new2",
                    Title = "老大亏了",
                    PicUrl = "http://c.hiphotos.baidu.com/ting/pic/item/b8014a90f603738d538032bfb21bb051f919ec61.jpg",
                    Url = "http://baidu.com"
                },
                new NewsItem
                {
                    Description = "new2",
                    Title = "老二霉了",
                    PicUrl = "http://c.hiphotos.baidu.com/ting/pic/item/b8014a90f603738d538032bfb21bb051f919ec61.jpg",
                    Url = "http://baidu.com"
                }
            }
        };
        return r;
    }
    
}

其次,在global.asax里注册处理逻辑

    void Application_Start(object sender, EventArgs e) 
    { 
        //注册事件处理程序
        ReceiveMessage.ResisterHandler(new WxMsgHandler());
    }

最后,在类似Page_Load等需要处理消息的地方处理消息

var m = ReceiveMessage.ParseFromContext(); 
var r = m.Process(); 
r.Response();  

3步搞定。其中,第1、2两步不再变化,只需将第3步中的代码写到需要处理消息的地方即可。