-
Notifications
You must be signed in to change notification settings - Fork 29
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;
}
}
void Application_Start(object sender, EventArgs e)
{
//注册事件处理程序
ReceiveMessage.ResisterHandler(new WxMsgHandler());
}
var m = ReceiveMessage.ParseFromContext();
var r = m.Process();
r.Response();