转载

用c#开发微信(3)基于Senparc.Weixin框架的接收普通消息处理 (源码下载)

本文讲述使用Senparc.Weixin框架来快速处理各种接收的普通消息。这里的消息指的是传统的微信公众平台消息交互,微信用户向公众号发送消息后,公众号回复消息给微信用户。包括以下7种类型:

1 文本消息

2 图片消息

3 语音消息

4 视频消息

5 小视频消息

6 地理位置消息

7 链接消息

实现非常简单,自定义一个继承MessageHandler的类,重写这7种类型的方法即可。注意: DefaultResponseMessage必须重写,用于返回没有处理过的消息类型(也可以用于默认消息,如帮助信息等);其中所有原OnXX的抽象方法已经都改为虚方法,可以不必每个都重写。若不重写,默认返回DefaultResponseMessage方法中的结果。

下面详细介绍实现步骤:

1. 添加index页面

private readonly string Token = ConfigurationManager.AppSettings["token"];//与微信公众账号后台的Token设置保持一致,区分大小写。
protected void Page_Load(object sender, EventArgs e)
{
string signature = Request["signature"];
string timestamp = Request["timestamp"];
string nonce = Request["nonce"];
string echostr = Request["echostr"];
if (Request.HttpMethod == "GET")
{
//get method - 仅在微信后台填写URL验证时触发
if (CheckSignature.Check(signature, timestamp, nonce, Token))
{
WriteContent(echostr); //返回随机字符串则表示验证通过
}
else
{
WriteContent("failed:" + signature + "," + CheckSignature.GetSignature(timestamp, nonce, Token) + "。" +
"如果你在浏览器中看到这句话,说明此地址可以被作为微信公众账号后台的Url,请注意保持Token一致。");
}
Response.End();
}
else
{
//post method - 当有用户想公众账号发送消息时触发
if (!CheckSignature.Check(signature, timestamp, nonce, Token))
{
WriteContent("参数错误!");
return;
}
//设置每个人上下文消息储存的最大数量,防止内存占用过多,如果该参数小于等于0,则不限制
var maxRecordCount = 10;
//自定义MessageHandler,对微信请求的详细判断操作都在这里面。
var messageHandler = new CustomMessageHandler(Request.InputStream, maxRecordCount);
try
{
//测试时可开启此记录,帮助跟踪数据,使用前请确保App_Data文件夹存在,且有读写权限。
messageHandler.RequestDocument.Save(
Server.MapPath("~/App_Data/" + DateTime.Now.Ticks + "_Request_" +
messageHandler.RequestMessage.FromUserName + ".txt"));
//执行微信处理过程
messageHandler.Execute();
//测试时可开启,帮助跟踪数据
messageHandler.ResponseDocument.Save(
Server.MapPath("~/App_Data/" + DateTime.Now.Ticks + "_Response_" +
messageHandler.ResponseMessage.ToUserName + ".txt"));
WriteContent(messageHandler.ResponseDocument.ToString());
return;
}
catch (Exception ex)
{
//将程序运行中发生的错误记录到App_Data文件夹
using (TextWriter tw = new StreamWriter(Server.MapPath("~/App_Data/Error_" + DateTime.Now.Ticks + ".txt")))
{
tw.WriteLine(ex.Message);
tw.WriteLine(ex.InnerException.Message);
if (messageHandler.ResponseDocument != null)
{
tw.WriteLine(messageHandler.ResponseDocument.ToString());
}
tw.Flush();
tw.Close();
}
WriteContent("");
}
finally
{
Response.End();
}
}
}
private void WriteContent(string str)
{
Response.Output.Write(str);
}

1)当Get请求时,调用 CheckSignature.Check(signature, timestamp, nonce, Token) 方法验证url接入, 详情参考 用c#开发微信(1)服务号的服务器配置和企业号的回调模式 - url接入 (源码下载)

2)  当有Post请求过来时,调用自定义MessageHandler类,对微信请求的详细判断操作都在这里面。

var messageHandler = new CustomMessageHandler(Request.InputStream, maxRecordCount);

messageHandler.Execute();

2. 自定义消息处理类

定义CustomMessageHandler继承MessageHandler<MessageContext<IRequestMessageBase, IResponseMessageBase>>

public partial class CustomMessageHandler : MessageHandler<MessageContext<IRequestMessageBase, IResponseMessageBase>>
{
public CustomMessageHandler(Stream inputStream, int maxRecordCount = 0)
: base(inputStream, null, maxRecordCount)
{
WeixinContext.ExpireMinutes = 3;
}
public override void OnExecuting()
{
//测试MessageContext.StorageData
if (CurrentMessageContext.StorageData == null)
{
CurrentMessageContext.StorageData = 0;
}
base.OnExecuting();
}
public override void OnExecuted()
{
base.OnExecuted();
CurrentMessageContext.StorageData = ((int)CurrentMessageContext.StorageData) + 1;
}
}

3. 分别重写7种接收普通消息

我们可以通过重写MessageHandler里的这7种类型方法来处理我们的业务,当然也可以只重写需要的部分类型,不需要的类型可以不重写,只需要定义一个统一的DefaultResponseMessage

public override IResponseMessageBase DefaultResponseMessage(IRequestMessageBase requestMessage)
{
//所有没有被处理的消息会默认返回这里的结果
var responseMessage = this.CreateResponseMessage<ResponseMessageText>();
responseMessage.Content = "这条消息来自DefaultResponseMessage。";
return responseMessage;
}

下面分别就这7种类型,各写一个例子:

1) 文本消息

/// <summary>
/// 处理文字请求
/// </summary>
/// <returns></returns>
public override IResponseMessageBase OnTextRequest(RequestMessageText requestMessage)
{
//注意:下面泛型ResponseMessageText即返回给客户端的类型,可以根据自己的需要填写ResponseMessageNews等不同类型。
var responseMessage = CreateResponseMessage<ResponseMessageText>();
var result = new StringBuilder();
result.AppendFormat("您刚才发送了文字信息:{0}/r/n/r/n", requestMessage.Content);
if (CurrentMessageContext.RequestMessages.Count > 1)
{
result.AppendFormat("您刚才还发送了如下消息({0}/{1}):/r/n", CurrentMessageContext.RequestMessages.Count, CurrentMessageContext.StorageData);
for (int i = CurrentMessageContext.RequestMessages.Count - 2; i >= 0; i--)
{
var historyMessage = CurrentMessageContext.RequestMessages[i];
result.AppendFormat("{0} 【{1}】{2}/r/n",
historyMessage.CreateTime.ToShortTimeString(),
historyMessage.MsgType.ToString(),
(historyMessage is RequestMessageText)
? (historyMessage as RequestMessageText).Content
: "[非文字类型]"
);
}
result.AppendLine("/r/n");
}
result.AppendFormat("如果您在{0}分钟内连续发送消息,记录将被自动保留(当前设置:最多记录{1}条)。过期后记录将会自动清除。/r/n", WeixinContext.ExpireMinutes, WeixinContext.MaxRecordCount);
result.AppendLine("/r/n");
result.AppendLine("您还可以发送【位置】【图片】【语音】【视频】等类型的信息(注意是这几种类型,不是这几个文字),查看不同格式的回复。");
responseMessage.Content = result.ToString();
return responseMessage;
}

2) 图片消息

/// <summary>
/// 处理图片请求
/// </summary>
/// <param name="requestMessage"></param>
/// <returns></returns>
public override IResponseMessageBase OnImageRequest(RequestMessageImage requestMessage)
{
var responseMessage = CreateResponseMessage<ResponseMessageNews>();
responseMessage.Articles.Add(new Article()
{
Title = "您刚才发送了图片信息",
Description = "您发送的图片将会显示在边上",
PicUrl = requestMessage.PicUrl,
Url = "http://www.hp.com"
});
responseMessage.Articles.Add(new Article()
{
Title = "第二条",
Description = "第二条带连接的内容",
PicUrl = requestMessage.PicUrl,
Url = "http://www.hp.com"
});
return responseMessage;
}

3) 语音消息

/// <summary>
/// 处理语音请求
/// </summary>
/// <param name="requestMessage"></param>
/// <returns></returns>
public override IResponseMessageBase OnVoiceRequest(RequestMessageVoice requestMessage)
{
var responseMessage = CreateResponseMessage<ResponseMessageMusic>();
responseMessage.Music.MusicUrl = "http://www.qxuninfo.com/music.mp3";
responseMessage.Music.Title = "这里是一条音乐消息";
responseMessage.Music.Description = "时间都去哪儿了";
return responseMessage;
}

4) 视频消息

/// <summary>
/// 处理视频请求
/// </summary>
/// <param name="requestMessage"></param>
/// <returns></returns>
public override IResponseMessageBase OnVideoRequest(RequestMessageVideo requestMessage)
{
var responseMessage = CreateResponseMessage<ResponseMessageText>();
responseMessage.Content = "您发送了一条视频信息,ID:" + requestMessage.MediaId;
return responseMessage;
}

5) 小视频消息

/// <summary>
/// 处理小视频请求
/// </summary>
/// <param name="requestMessage"></param>
/// <returns></returns>
public override IResponseMessageBase OnShortVideoRequest(RequestMessageShortVideo requestMessage)
{
var responseMessage = this.CreateResponseMessage<ResponseMessageText>();
responseMessage.Content = "您刚才发送的是小视频";
return responseMessage;
}

6) 地理位置消息

/// <summary>
/// 处理位置请求
/// </summary>
/// <param name="requestMessage"></param>
/// <returns></returns>
public override IResponseMessageBase OnLocationRequest(RequestMessageLocation requestMessage)
{
var responseMessage = CreateResponseMessage<ResponseMessageText>();
responseMessage.Content = string.Format("您刚才发送了地理位置信息。Location_X:{0},Location_Y:{1},Scale:{2},标签:{3}",
requestMessage.Location_X, requestMessage.Location_Y,
requestMessage.Scale, requestMessage.Label);
return responseMessage;
}

7) 链接消息

/// <summary>
/// 处理链接消息请求
/// </summary>
/// <param name="requestMessage"></param>
/// <returns></returns>
public override IResponseMessageBase OnLinkRequest(RequestMessageLink requestMessage)
{
var responseMessage = CreateResponseMessage<ResponseMessageText>();
responseMessage.Content = string.Format(@"您发送了一条连接信息:
Title:{0}
Description:{1}
Url:{2}", requestMessage.Title, requestMessage.Description, requestMessage.Url);
return responseMessage;
}

从上面的例子中可以看出,回复的消息类型可以多种多样,不一定要跟请求的消息类型一样。

  • 框架里的请求类型:

用c#开发微信(3)基于Senparc.Weixin框架的接收普通消息处理 (源码下载)

  • 框架里的回复类型:

用c#开发微信(3)基于Senparc.Weixin框架的接收普通消息处理 (源码下载)

4. 最后发布到自己的服务器上,可以尝试给公众号发各种类型的消息,验证公众号回复的内容

5. 源码

最后整个程序结构如下:

用c#开发微信(3)基于Senparc.Weixin框架的接收普通消息处理 (源码下载)

源码下载: http://yunpan.cn/cwsftnRtzK599 访问密码 4247

同样的,使用源码前,要先把配置文件里的参数修改成自己的公众号。

部份类型效果截图:

用c#开发微信(3)基于Senparc.Weixin框架的接收普通消息处理 (源码下载)

官方文档: 接收普通消息

用c#开发微信 系列汇总

正文到此结束
Loading...