Senparc.Weixin SDK 是由盛派网络(Senparc)团队自主研发的针对微信各模块的
开发套件(C# SDK),
已全面支持微信公众号、微信支付、企业号、开放平台、JSSDK、摇一摇周边等模块。
快使用 Senparc.Weixin SDK 轻松打造微信各平台的扩展应用吧!
如何把SDK引入到项目中 看http://www.cnblogs.com/szw/p/3414732.html
引入到项目中过后我们开始进行微信的信息处理。
示例项目为ASP.NET MVC。
1.登陆公众号,然后打开基本配置。
2.创建一个控制器用于接收微信post过来的数据
public class WebChartAutoController : Controller
{
public static readonly string Token = Config.Token;//与微信公众账号后台的Token设置保持一致,区分大小写。
public static readonly string EncodingAESKey =Config.EncodingAESKey;//与微信公众账号后台的EncodingAESKey设置保持一致,区分大小写。
public static readonly string AppId = Config.AppID;//与微信公众账号后台的AppId设置保持一致,区分大小写。
[HttpGet]
[ActionName("Index")]
public ActionResult Get(PostModel postModel, string echostr)
{
if (CheckSignature.Check(postModel.Signature, postModel.Timestamp, postModel.Nonce, Token))
{
return Content(echostr); //返回随机字符串则表示验证通过
}
else
{
return Content("failed:" + postModel.Signature + "," + Senparc.Weixin.MP.CheckSignature.GetSignature(postModel.Timestamp, postModel.Nonce, Token) + "。" +
"如果你在浏览器中看到这句话,说明此地址可以被作为微信公众账号后台的Url,请注意保持Token一致。");
}
}
/// <summary>
/// 最简化的处理流程(不加密)
/// </summary>
[HttpPost]
[ActionName("Index")]
public ActionResult Index(PostModel postModel)
{
if (!CheckSignature.Check(postModel.Signature, postModel.Timestamp, postModel.Nonce, Token))
{
//return Content("参数错误!");//v0.7-
return new WeixinResult("参数错误!");//v0.8+
}
postModel.Token = Token;
postModel.EncodingAESKey = EncodingAESKey;//根据自己后台的设置保持一致
postModel.AppId = AppId;//根据自己后台的设置保持一致
var messageHandler = new CustomMessageHandler(Request.InputStream, postModel, 10);
messageHandler.Execute();//执行微信处理过程
//return Content(messageHandler.ResponseDocument.ToString());//v0.7-
return new FixWeixinBugWeixinResult(messageHandler);//v0.8+
}
}
}
3.创建一个CustomMessageHandler信息处理机制来处理微信客户端POST过来的数据。
/// <summary>
/// 自定义MessageHandler
/// 把MessageHandler作为基类,重写对应请求的处理方法
/// </summary>
public partial class CustomMessageHandler : MessageHandler<CustomMessageContext>
{
/*
* 重要提示:v1.5起,MessageHandler提供了一个DefaultResponseMessage的抽象方法,
* DefaultResponseMessage必须在子类中重写,用于返回没有处理过的消息类型(也可以用于默认消息,如帮助信息等);
* 其中所有原OnXX的抽象方法已经都改为虚方法,可以不必每个都重写。若不重写,默认返回DefaultResponseMessage方法中的结果。
*/
#if DEBUG
string agentUrl = "http://localhost:12222/App/Weixin/4";
string agentToken = "27C455F496044A87";
string wiweihiKey = "CNadjJuWzyX5bz5Gn+/XoyqiqMa5DjXQ";
#else
//下面的Url和Token可以用其他平台的消息,或者到www.weiweihi.com注册微信用户,将自动在“微信营销工具”下得到
private string agentUrl = WebConfigurationManager.AppSettings["WeixinAgentUrl"];//这里使用了www.weiweihi.com微信自动托管平台
private string agentToken = WebConfigurationManager.AppSettings["WeixinAgentToken"];//Token
private string wiweihiKey = WebConfigurationManager.AppSettings["WeixinAgentWeiweihiKey"];//WeiweihiKey专门用于对接www.Weiweihi.com平台,获取方式见:http://www.weiweihi.com/ApiDocuments/Item/25#51
#endif
private string appId = WebConfigurationManager.AppSettings["WeixinAppId"];
private string appSecret = WebConfigurationManager.AppSettings["WeixinAppSecret"];
/// <summary>
/// 模板消息集合(Key:checkCode,Value:OpenId)
/// </summary>
public static Dictionary<string, string> TemplateMessageCollection = new Dictionary<string, string>();
public CustomMessageHandler(Stream inputStream, PostModel postModel, int maxRecordCount = 0)
: base(inputStream, postModel, maxRecordCount)
{
//这里设置仅用于测试,实际开发可以在外部更全局的地方设置,
//比如MessageHandler<MessageContext>.GlobalWeixinContext.ExpireMinutes = 3。
WeixinContext.ExpireMinutes = 3;
if (!string.IsNullOrEmpty(postModel.AppId))
{
appId = postModel.AppId;//通过第三方开放平台发送过来的请求
}
//在指定条件下,不使用消息去重
base.OmitRepeatedMessageFunc = requestMessage =>
{
var textRequestMessage = requestMessage as RequestMessageText;
if (textRequestMessage != null && textRequestMessage.Content == "容错")
{
return false;
}
return true;
};
}
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;
}
/// <summary>
/// 处理文字请求
/// </summary>
/// <returns></returns>
public override IResponseMessageBase OnTextRequest(RequestMessageText requestMessage)
{
//TODO:这里的逻辑可以交给Service处理具体信息,参考OnLocationRequest方法或/Service/LocationSercice.cs
//书中例子
//if (requestMessage.Content == "你好")
//{
// var responseMessage = base.CreateResponseMessage<ResponseMessageNews>();
// var title = "Title";
// var description = "Description";
// var picUrl = "PicUrl";
// var url = "Url";
// responseMessage.Articles.Add(new Article()
// {
// Title = title,
// Description = description,
// PicUrl = picUrl,
// Url = url
// });
// return responseMessage;
//}
//else if (requestMessage.Content == "Senparc")
//{
// //相似处理逻辑
//}
//else
//{
// //...
//}
//方法一(v0.1),此方法调用太过繁琐,已过时(但仍是所有方法的核心基础),建议使用方法二到四
//var responseMessage =
// ResponseMessageBase.CreateFromRequestMessage(RequestMessage, ResponseMsgType.Text) as
// ResponseMessageText;
//方法二(v0.4)
//var responseMessage = ResponseMessageBase.CreateFromRequestMessage<ResponseMessageText>(RequestMessage);
//方法三(v0.4),扩展方法,需要using Senparc.Weixin.MP.Helpers;
//var responseMessage = RequestMessage.CreateResponseMessage<ResponseMessageText>();
//方法四(v0.6+),仅适合在HandlerMessage内部使用,本质上是对方法三的封装
//注意:下面泛型ResponseMessageText即返回给客户端的类型,可以根据自己的需要填写ResponseMessageNews等不同类型。
var responseMessage = base.CreateResponseMessage<ResponseMessageText>();
LogHelper.WriteLog(null, "CustomMessageHandler"+ requestMessage.Content);
if (requestMessage.Content == null)
{
}
else if (requestMessage.Content == "约束")
{
responseMessage.Content =
@"您正在进行微信内置浏览器约束判断测试。。";
}
else if (requestMessage.Content == "托管" || requestMessage.Content == "代理")
{
//开始用代理托管,把请求转到其他服务器上去,然后拿回结果
//甚至也可以将所有请求在DefaultResponseMessage()中托管到外部。
DateTime dt1 = DateTime.Now; //计时开始
var responseXml = MessageAgent.RequestXml(this, agentUrl, agentToken, RequestDocument.ToString());
//获取返回的XML
//上面的方法也可以使用扩展方法:this.RequestResponseMessage(this,agentUrl, agentToken, RequestDocument.ToString());
/* 如果有WeiweihiKey,可以直接使用下面的这个MessageAgent.RequestWeiweihiXml()方法。
* WeiweihiKey专门用于对接www.weiweihi.com平台,获取方式见:http://www.weiweihi.com/ApiDocuments/Item/25#51
*/
//var responseXml = MessageAgent.RequestWeiweihiXml(weiweihiKey, RequestDocument.ToString());//获取Weiweihi返回的XML
DateTime dt2 = DateTime.Now; //计时结束
//转成实体。
/* 如果要写成一行,可以直接用:
* responseMessage = MessageAgent.RequestResponseMessage(agentUrl, agentToken, RequestDocument.ToString());
* 或
*
*/
var msg = string.Format("\r\n\r\n代理过程总耗时:{0}毫秒", (dt2 - dt1).Milliseconds);
var agentResponseMessage = responseXml.CreateResponseMessage();
if (agentResponseMessage is ResponseMessageText)
{
(agentResponseMessage as ResponseMessageText).Content += msg;
}
else if (agentResponseMessage is ResponseMessageNews)
{
(agentResponseMessage as ResponseMessageNews).Articles[0].Description += msg;
}
return agentResponseMessage;//可能出现多种类型,直接在这里返回
}
else if (requestMessage.Content == "测试" || requestMessage.Content == "退出")
{
/*
* 这是一个特殊的过程,此请求通常来自于微微嗨(http://www.weiweihi.com)的“盛派网络小助手”应用请求(http://www.weiweihi.com/User/App/Detail/1),
* 用于演示微微嗨应用商店的处理过程,由于微微嗨的应用内部可以单独设置对话过期时间,所以这里通常不需要考虑对话状态,只要做最简单的响应。
*/
if (requestMessage.Content == "测试")
{
//进入APP测试
responseMessage.Content = "您已经进入。";
}
else
{
//退出APP测试
responseMessage.Content = "您已经退出。";
}
}
else if (requestMessage.Content == "AsyncTest")
{
//异步并发测试(提供给单元测试使用)
DateTime begin = DateTime.Now;
int t1, t2, t3;
System.Threading.ThreadPool.GetAvailableThreads(out t1, out t3);
System.Threading.ThreadPool.GetMaxThreads(out t2, out t3);
System.Threading.Thread.Sleep(TimeSpan.FromSeconds(4));
DateTime end = DateTime.Now;
var thread = System.Threading.Thread.CurrentThread;
responseMessage.Content = string.Format("TId:{0}\tApp:{1}\tBegin:{2:mm:ss,ffff}\tEnd:{3:mm:ss,ffff}\tTPool:{4}",
thread.ManagedThreadId,
HttpContext.Current != null ? HttpContext.Current.ApplicationInstance.GetHashCode() : -1,
begin,
end,
t2 - t1
);
}
else if (requestMessage.Content.ToUpper() == "OPEN")
{
var openResponseMessage = requestMessage.CreateResponseMessage<ResponseMessageNews>();
openResponseMessage.Articles.Add(new Article()
{
Title = "开放平台微信授权测试",
Description = @"点击进入Open授权页面。。",
Url = "http://sdk.weixin.senparc.com/OpenOAuth/JumpToMpOAuth"
});
return openResponseMessage;
}
else if (requestMessage.Content == "错误")
{
var errorResponseMessage = requestMessage.CreateResponseMessage<ResponseMessageText>();
//因为没有设置errorResponseMessage.Content,所以这小消息将无法正确返回。
return errorResponseMessage;
}
else if (requestMessage.Content == "容错")
{
Thread.Sleep(1500);//故意延时1.5秒,让微信多次发送消息过来,观察返回结果
var faultTolerantResponseMessage = requestMessage.CreateResponseMessage<ResponseMessageText>();
faultTolerantResponseMessage.Content = string.Format("测试容错,MsgId:{0},Ticks:{1}", requestMessage.MsgId,
DateTime.Now.Ticks);
return faultTolerantResponseMessage;
}
else if (requestMessage.Content.ToUpper() == "TM")//异步模板消息设置
{
var openId = requestMessage.FromUserName;
var checkCode = Guid.NewGuid().ToString("n").Substring(0, 3);//为了防止openId泄露造成骚扰,这里启用验证码
TemplateMessageCollection[checkCode] = openId;
responseMessage.Content = string.Format(@"新的验证码为:{0},请在网页上输入", checkCode);
}
else if (requestMessage.Content.ToUpper() == "OPENID") //返回OpenId及用户信息
{
var openId = requestMessage.FromUserName;//获取OpenId
var userInfo = AdvancedAPIs.UserApi.Info(appId, openId, Language.zh_CN);
responseMessage.Content = string.Format(
"您的OpenID为:{0}\r\n昵称:{1}\r\n性别:{2}\r\n地区(国家/省/市):{3}/{4}/{5}\r\n关注时间:{6}\r\n关注状态:{7}",
requestMessage.FromUserName, userInfo.nickname, (Sex)userInfo.sex, userInfo.country, userInfo.province, userInfo.city, DateTimeHelper.GetDateTimeFromXml(userInfo.subscribe_time), userInfo.subscribe);
}
else if (requestMessage.Content.ToUpper() == "EX")
{
var ex = new WeixinException("openid:" + requestMessage.FromUserName + ":这是一条测试异常信息");//回调过程在global的ConfigWeixinTraceLog()方法中
responseMessage.Content = "请等待异步模板消息发送到此界面上(自动延时数秒)。\r\n当前时间:" + DateTime.Now.ToString();
}
else
{
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(
"您还可以发送【位置】【图片】【语音】【视频】等类型的信息(注意是这几种类型,不是这几个文字),查看不同格式的回复。\r\nSDK官方地址:http://sdk.weixin.senparc.com");
responseMessage.Content = result.ToString();
}
return responseMessage;
}
/// <summary>
/// 处理位置请求
/// </summary>
/// <param name="requestMessage"></param>
/// <returns></returns>
public override IResponseMessageBase OnLocationRequest(RequestMessageLocation requestMessage)
{
var locationService = new LocationService();
var responseMessage = locationService.GetResponseMessage(requestMessage as RequestMessageLocation);
return responseMessage;
}
public override IResponseMessageBase OnShortVideoRequest(RequestMessageShortVideo requestMessage)
{
var responseMessage = this.CreateResponseMessage<ResponseMessageText>();
responseMessage.Content = "您刚才发送的是小视频";
return responseMessage;
}
/// <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://sdk.weixin.senparc.com"
});
responseMessage.Articles.Add(new Article()
{
Title = "第二条",
Description = "第二条带连接的内容",
PicUrl = requestMessage.PicUrl,
Url = "http://sdk.weixin.senparc.com"
});
return responseMessage;
}
/// <summary>
/// 处理语音请求
/// </summary>
/// <param name="requestMessage"></param>
/// <returns></returns>
public override IResponseMessageBase OnVoiceRequest(RequestMessageVoice requestMessage)
{
var responseMessage = CreateResponseMessage<ResponseMessageMusic>();
//上传缩略图
var accessToken = Containers.AccessTokenContainer.TryGetAccessToken(appId, appSecret);
var uploadResult = AdvancedAPIs.MediaApi.UploadTemporaryMedia(accessToken, UploadMediaFileType.image, System.Web.HttpContext.Current.Server.MapPath("~/Images/Logo.jpg"));
//设置音乐信息
responseMessage.Music.Title = "天籁之音";
responseMessage.Music.Description = "播放您上传的语音";
responseMessage.Music.MusicUrl = "http://sdk.weixin.senparc.com/Media/GetVoice?mediaId=" + requestMessage.MediaId;
responseMessage.Music.HQMusicUrl = "http://sdk.weixin.senparc.com/Media/GetVoice?mediaId=" + requestMessage.MediaId;
responseMessage.Music.ThumbMediaId = uploadResult.media_id;
return responseMessage;
}
/// <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;
}
/// <summary>
/// 处理链接消息请求
/// </summary>
/// <param name="requestMessage"></param>
/// <returns></returns>
public override IResponseMessageBase OnLinkRequest(RequestMessageLink requestMessage)
{
var responseMessage = ResponseMessageBase.CreateFromRequestMessage<ResponseMessageText>(requestMessage);
responseMessage.Content = string.Format(@"您发送了一条连接信息:
Title:{0}
Description:{1}
Url:{2}", requestMessage.Title, requestMessage.Description, requestMessage.Url);
return responseMessage;
}
/// <summary>
/// 处理事件请求(这个方法一般不用重写,这里仅作为示例出现。除非需要在判断具体Event类型以外对Event信息进行统一操作
/// </summary>
/// <param name="requestMessage"></param>
/// <returns></returns>
public override IResponseMessageBase OnEventRequest(IRequestMessageEventBase requestMessage)
{
var eventResponseMessage = base.OnEventRequest(requestMessage);//对于Event下属分类的重写方法,见:CustomerMessageHandler_Events.cs
//TODO: 对Event信息进行统一操作
return eventResponseMessage;
}
public override IResponseMessageBase DefaultResponseMessage(IRequestMessageBase requestMessage)
{
/* 所有没有被处理的消息会默认返回这里的结果,
* 因此,如果想把整个微信请求委托出去(例如需要使用分布式或从其他服务器获取请求),
* 只需要在这里统一发出委托请求,如:
* var responseMessage = MessageAgent.RequestResponseMessage(agentUrl, agentToken, RequestDocument.ToString());
* return responseMessage;
*/
var responseMessage = this.CreateResponseMessage<ResponseMessageText>();
responseMessage.Content = "这条消息来自DefaultResponseMessage。";
return responseMessage;
}
}
}
这儿是针对于Event下属分类的重写方法
/*----------------------------------------------------------------
Copyright (C) 2017 Senparc
文件名:CustomMessageHandler_Events.cs
文件功能描述:自定义MessageHandler
创建标识:Senparc - 20150312
----------------------------------------------------------------*/
using System;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using Senparc.Weixin.MP.Agent;
using Senparc.Weixin.Context;
using Senparc.Weixin.Exceptions;
using Senparc.Weixin.HttpUtility;
using Senparc.Weixin.MP.Entities;
using Senparc.Weixin.MP.Helpers;
using Senparc.Weixin.MP.MessageHandlers;
using Senparc.Weixin.MP.Sample.CommonService.Download;
namespace Senparc.Weixin.MP.Sample.CommonService.CustomMessageHandler
{
/// <summary>
/// 自定义MessageHandler
/// </summary>
public partial class CustomMessageHandler
{
private string GetWelcomeInfo()
{
//获取Senparc.Weixin.MP.dll版本信息
var fileVersionInfo = FileVersionInfo.GetVersionInfo(HttpContext.Current.Server.MapPath("~/bin/Senparc.Weixin.MP.dll"));
var version = string.Format("{0}.{1}.{2}", fileVersionInfo.FileMajorPart, fileVersionInfo.FileMinorPart, fileVersionInfo.FileBuildPart);
return string.Format(
@"欢迎关注【Senparc.Weixin.MP 微信公众平台SDK】,当前运行版本:v{0}。
您可以发送【文字】【位置】【图片】【语音】等不同类型的信息,查看不同格式的回复。
您也可以直接点击菜单查看各种类型的回复。
还可以点击菜单体验微信支付。
SDK官方地址:https://weixin.senparc.com
SDK Demo:http://sdk.weixin.senparc.com
源代码及Demo下载地址:https://github.com/JeffreySu/WeiXinMPSDK
Nuget地址:https://www.nuget.org/packages/Senparc.Weixin.MP
QQ群:342319110
===============
更多:
1、JSSDK测试:http://sdk.weixin.senparc.com/WeixinJSSDK
2、开放平台测试(建议PC上打开):http://sdk.weixin.senparc.com/OpenOAuth/JumpToMpOAuth
3、回复关键字:
【open】 进入第三方开放平台(Senparc.Weixin.Open)测试
【tm】 测试异步模板消息
【openid】 获取OpenId等用户信息
【约束】 测试微信浏览器约束
【AsyncTest】 异步并发测试
【错误】 体验发生错误无法返回正确信息
【容错】 体验去重容错
【ex】 体验错误日志推送提醒
",
version);
}
public string GetDownloadInfo(CodeRecord codeRecord)
{
return string.Format(@"您已通过二维码验证,浏览器即将开始下载 Senparc.Weixin SDK 帮助文档。
当前选择的版本:v{0}({1})
我们期待您的意见和建议,客服热线:400-031-8816。
感谢您对盛派网络的支持!
© 2016 Senparc", codeRecord.Version, codeRecord.IsWebVersion ? "网页版" : ".chm文档版");
}
public override IResponseMessageBase OnTextOrEventRequest(RequestMessageText requestMessage)
{
// 预处理文字或事件类型请求。
// 这个请求是一个比较特殊的请求,通常用于统一处理来自文字或菜单按钮的同一个执行逻辑,
// 会在执行OnTextRequest或OnEventRequest之前触发,具有以下一些特征:
// 1、如果返回null,则继续执行OnTextRequest或OnEventRequest
// 2、如果返回不为null,则终止执行OnTextRequest或OnEventRequest,返回最终ResponseMessage
// 3、如果是事件,则会将RequestMessageEvent自动转为RequestMessageText类型,其中RequestMessageText.Content就是RequestMessageEvent.EventKey
if (requestMessage.Content == "OneClick")
{
var strongResponseMessage = CreateResponseMessage<ResponseMessageText>();
strongResponseMessage.Content = "您点击了底部按钮。\r\n为了测试微信软件换行bug的应对措施,这里做了一个——\r\n换行";
return strongResponseMessage;
}
return null;//返回null,则继续执行OnTextRequest或OnEventRequest
}
public override IResponseMessageBase OnEvent_ClickRequest(RequestMessageEvent_Click requestMessage)
{
IResponseMessageBase reponseMessage = null;
//菜单点击,需要跟创建菜单时的Key匹配
switch (requestMessage.EventKey)
{
case "OneClick":
{
//这个过程实际已经在OnTextOrEventRequest中完成,这里不会执行到。
var strongResponseMessage = CreateResponseMessage<ResponseMessageText>();
reponseMessage = strongResponseMessage;
strongResponseMessage.Content = "您点击了底部按钮。\r\n为了测试微信软件换行bug的应对措施,这里做了一个——\r\n换行";
}
break;
case "SubClickRoot_Text":
{
var strongResponseMessage = CreateResponseMessage<ResponseMessageText>();
reponseMessage = strongResponseMessage;
strongResponseMessage.Content = "您点击了子菜单按钮。";
}
break;
case "SubClickRoot_News":
{
var strongResponseMessage = CreateResponseMessage<ResponseMessageNews>();
reponseMessage = strongResponseMessage;
strongResponseMessage.Articles.Add(new Article()
{
Title = "您点击了子菜单图文按钮",
Description = "您点击了子菜单图文按钮,这是一条图文信息。",
PicUrl = "http://sdk.weixin.senparc.com/Images/qrcode.jpg",
Url = "http://sdk.weixin.senparc.com"
});
}
break;
case "SubClickRoot_Music":
{
//上传缩略图
var accessToken = Containers.AccessTokenContainer.TryGetAccessToken(appId, appSecret);
var uploadResult = AdvancedAPIs.MediaApi.UploadTemporaryMedia(accessToken, UploadMediaFileType.thumb,
System.Web.HttpContext.Current.Server.MapPath("~/Images/Logo.jpg"));
//设置音乐信息
var strongResponseMessage = CreateResponseMessage<ResponseMessageMusic>();
reponseMessage = strongResponseMessage;
strongResponseMessage.Music.Title = "天籁之音";
strongResponseMessage.Music.Description = "真的是天籁之音";
strongResponseMessage.Music.MusicUrl = "http://sdk.weixin.senparc.com/Content/music1.mp3";
strongResponseMessage.Music.HQMusicUrl = "http://sdk.weixin.senparc.com/Content/music1.mp3";
strongResponseMessage.Music.ThumbMediaId = uploadResult.thumb_media_id;
}
break;
case "SubClickRoot_Image":
{
//上传图片
var accessToken = Containers.AccessTokenContainer.TryGetAccessToken(appId, appSecret);
var uploadResult = AdvancedAPIs.MediaApi.UploadTemporaryMedia(accessToken, UploadMediaFileType.image,
System.Web.HttpContext.Current.Server.MapPath("~/Images/Logo.jpg"));
//设置图片信息
var strongResponseMessage = CreateResponseMessage<ResponseMessageImage>();
reponseMessage = strongResponseMessage;
strongResponseMessage.Image.MediaId = uploadResult.media_id;
}
break;
case "SubClickRoot_Agent"://代理消息
{
//获取返回的XML
DateTime dt1 = DateTime.Now;
reponseMessage = MessageAgent.RequestResponseMessage(this, agentUrl, agentToken, RequestDocument.ToString());
//上面的方法也可以使用扩展方法:this.RequestResponseMessage(this,agentUrl, agentToken, RequestDocument.ToString());
DateTime dt2 = DateTime.Now;
if (reponseMessage is ResponseMessageNews)
{
(reponseMessage as ResponseMessageNews)
.Articles[0]
.Description += string.Format("\r\n\r\n代理过程总耗时:{0}毫秒", (dt2 - dt1).Milliseconds);
}
}
break;
case "Member"://托管代理会员信息
{
//原始方法为:MessageAgent.RequestXml(this,agentUrl, agentToken, RequestDocument.ToString());//获取返回的XML
reponseMessage = this.RequestResponseMessage(agentUrl, agentToken, RequestDocument.ToString());
}
break;
case "OAuth"://OAuth授权测试
{
var strongResponseMessage = CreateResponseMessage<ResponseMessageNews>();
strongResponseMessage.Articles.Add(new Article()
{
Title = "OAuth2.0测试",
Description = "选择下面两种不同的方式进行测试,区别在于授权成功后,最后停留的页面。",
//Url = "http://sdk.weixin.senparc.com/oauth2",
//PicUrl = "http://sdk.weixin.senparc.com/Images/qrcode.jpg"
});
strongResponseMessage.Articles.Add(new Article()
{
Title = "OAuth2.0测试(不带returnUrl),测试环境可使用",
Description = "OAuth2.0测试(不带returnUrl)",
Url = "http://sdk.weixin.senparc.com/oauth2",
PicUrl = "http://sdk.weixin.senparc.com/Images/qrcode.jpg"
});
var returnUrl = "/OAuth2/TestReturnUrl";
strongResponseMessage.Articles.Add(new Article()
{
Title = "OAuth2.0测试(带returnUrl),生产环境强烈推荐使用",
Description = "OAuth2.0测试(带returnUrl)",
Url = "http://sdk.weixin.senparc.com/oauth2?returnUrl=" + returnUrl.UrlEncode(),
PicUrl = "http://sdk.weixin.senparc.com/Images/qrcode.jpg"
});
reponseMessage = strongResponseMessage;
}
break;
case "Description":
{
var strongResponseMessage = CreateResponseMessage<ResponseMessageText>();
strongResponseMessage.Content = GetWelcomeInfo();
reponseMessage = strongResponseMessage;
}
break;
case "SubClickRoot_PicPhotoOrAlbum":
{
var strongResponseMessage = CreateResponseMessage<ResponseMessageText>();
reponseMessage = strongResponseMessage;
strongResponseMessage.Content = "您点击了【微信拍照】按钮。系统将会弹出拍照或者相册发图。";
}
break;
case "SubClickRoot_ScancodePush":
{
var strongResponseMessage = CreateResponseMessage<ResponseMessageText>();
reponseMessage = strongResponseMessage;
strongResponseMessage.Content = "您点击了【微信扫码】按钮。";
}
break;
case "ConditionalMenu_Male":
{
var strongResponseMessage = CreateResponseMessage<ResponseMessageText>();
reponseMessage = strongResponseMessage;
strongResponseMessage.Content = "您点击了个性化菜单按钮,您的微信性别设置为:男。";
}
break;
case "ConditionalMenu_Femle":
{
var strongResponseMessage = CreateResponseMessage<ResponseMessageText>();
reponseMessage = strongResponseMessage;
strongResponseMessage.Content = "您点击了个性化菜单按钮,您的微信性别设置为:女。";
}
break;
default:
{
var strongResponseMessage = CreateResponseMessage<ResponseMessageText>();
strongResponseMessage.Content = "您点击了按钮,EventKey:" + requestMessage.EventKey;
reponseMessage = strongResponseMessage;
}
break;
}
return reponseMessage;
}
public override IResponseMessageBase OnEvent_EnterRequest(RequestMessageEvent_Enter requestMessage)
{
var responseMessage = ResponseMessageBase.CreateFromRequestMessage<ResponseMessageText>(requestMessage);
responseMessage.Content = "您刚才发送了ENTER事件请求。";
return responseMessage;
}
public override IResponseMessageBase OnEvent_LocationRequest(RequestMessageEvent_Location requestMessage)
{
//这里是微信客户端(通过微信服务器)自动发送过来的位置信息
var responseMessage = CreateResponseMessage<ResponseMessageText>();
responseMessage.Content = "这里写什么都无所谓,比如:上帝爱你!";
return responseMessage;//这里也可以返回null(需要注意写日志时候null的问题)
}
public override IResponseMessageBase OnEvent_ScanRequest(RequestMessageEvent_Scan requestMessage)
{
//通过扫描关注
var responseMessage = CreateResponseMessage<ResponseMessageText>();
//下载文档
//if (!string.IsNullOrEmpty(requestMessage.EventKey))
//{
// var sceneId = long.Parse(requestMessage.EventKey.Replace("qrscene_", ""));
// //var configHelper = new ConfigHelper(new HttpContextWrapper(HttpContext.Current));
// var codeRecord =
// ConfigHelper.CodeCollection.Values.FirstOrDefault(z => z.QrCodeTicket != null && z.QrCodeId == sceneId);
// if (codeRecord != null)
// {
// //确认可以下载
// codeRecord.AllowDownload = true;
// responseMessage.Content = GetDownloadInfo(codeRecord);
// }
//}
responseMessage.Content = responseMessage.Content ?? string.Format("通过扫描二维码进入,场景值:{0}", requestMessage.EventKey);
return responseMessage;
}
public override IResponseMessageBase OnEvent_ViewRequest(RequestMessageEvent_View requestMessage)
{
//说明:这条消息只作为接收,下面的responseMessage到达不了客户端,类似OnEvent_UnsubscribeRequest
var responseMessage = CreateResponseMessage<ResponseMessageText>();
responseMessage.Content = "您点击了view按钮,将打开网页:" + requestMessage.EventKey;
return responseMessage;
}
public override IResponseMessageBase OnEvent_MassSendJobFinishRequest(RequestMessageEvent_MassSendJobFinish requestMessage)
{
var responseMessage = CreateResponseMessage<ResponseMessageText>();
responseMessage.Content = "接收到了群发完成的信息。";
return responseMessage;
}
/// <summary>
/// 订阅(关注)事件
/// </summary>
/// <returns></returns>
public override IResponseMessageBase OnEvent_SubscribeRequest(RequestMessageEvent_Subscribe requestMessage)
{
var responseMessage = ResponseMessageBase.CreateFromRequestMessage<ResponseMessageText>(requestMessage);
responseMessage.Content = GetWelcomeInfo();
if (!string.IsNullOrEmpty(requestMessage.EventKey))
{
responseMessage.Content += "\r\n============\r\n场景值:" + requestMessage.EventKey;
}
//推送消息
//下载文档
if (requestMessage.EventKey.StartsWith("qrscene_"))
{
var sceneId = long.Parse(requestMessage.EventKey.Replace("qrscene_", ""));
//var configHelper = new ConfigHelper(new HttpContextWrapper(HttpContext.Current));
//var codeRecord =
// ConfigHelper.CodeCollection.Values.FirstOrDefault(z => z.QrCodeTicket != null && z.QrCodeId == sceneId);
//if (codeRecord != null)
//{
// if (codeRecord.AllowDownload)
// {
// Task.Factory.StartNew(() => AdvancedAPIs.CustomApi.SendTextAsync(null, WeixinOpenId, "下载已经开始,如需下载其他版本,请刷新页面后重新扫一扫。"));
// }
// else
// {
// //确认可以下载
// codeRecord.AllowDownload = true;
// Task.Factory.StartNew(() => AdvancedAPIs.CustomApi.SendTextAsync(null, WeixinOpenId, GetDownloadInfo(codeRecord)));
// }
//}
}
return responseMessage;
}
/// <summary>
/// 退订
/// 实际上用户无法收到非订阅账号的消息,所以这里可以随便写。
/// unsubscribe事件的意义在于及时删除网站应用中已经记录的OpenID绑定,消除冗余数据。并且关注用户流失的情况。
/// </summary>
/// <returns></returns>
public override IResponseMessageBase OnEvent_UnsubscribeRequest(RequestMessageEvent_Unsubscribe requestMessage)
{
var responseMessage = base.CreateResponseMessage<ResponseMessageText>();
responseMessage.Content = "有空再来";
return responseMessage;
}
/// <summary>
/// 事件之扫码推事件(scancode_push)
/// </summary>
/// <param name="requestMessage"></param>
/// <returns></returns>
public override IResponseMessageBase OnEvent_ScancodePushRequest(RequestMessageEvent_Scancode_Push requestMessage)
{
var responseMessage = base.CreateResponseMessage<ResponseMessageText>();
responseMessage.Content = "事件之扫码推事件";
return responseMessage;
}
/// <summary>
/// 事件之扫码推事件且弹出“消息接收中”提示框(scancode_waitmsg)
/// </summary>
/// <param name="requestMessage"></param>
/// <returns></returns>
public override IResponseMessageBase OnEvent_ScancodeWaitmsgRequest(RequestMessageEvent_Scancode_Waitmsg requestMessage)
{
var responseMessage = base.CreateResponseMessage<ResponseMessageText>();
responseMessage.Content = "事件之扫码推事件且弹出“消息接收中”提示框";
return responseMessage;
}
/// <summary>
/// 事件之弹出拍照或者相册发图(pic_photo_or_album)
/// </summary>
/// <param name="requestMessage"></param>
/// <returns></returns>
public override IResponseMessageBase OnEvent_PicPhotoOrAlbumRequest(RequestMessageEvent_Pic_Photo_Or_Album requestMessage)
{
var responseMessage = base.CreateResponseMessage<ResponseMessageText>();
responseMessage.Content = "事件之弹出拍照或者相册发图";
return responseMessage;
}
/// <summary>
/// 事件之弹出系统拍照发图(pic_sysphoto)
/// 实际测试时发现微信并没有推送RequestMessageEvent_Pic_Sysphoto消息,只能接收到用户在微信中发送的图片消息。
/// </summary>
/// <param name="requestMessage"></param>
/// <returns></returns>
public override IResponseMessageBase OnEvent_PicSysphotoRequest(RequestMessageEvent_Pic_Sysphoto requestMessage)
{
var responseMessage = base.CreateResponseMessage<ResponseMessageText>();
responseMessage.Content = "事件之弹出系统拍照发图";
return responseMessage;
}
/// <summary>
/// 事件之弹出微信相册发图器(pic_weixin)
/// </summary>
/// <param name="requestMessage"></param>
/// <returns></returns>
public override IResponseMessageBase OnEvent_PicWeixinRequest(RequestMessageEvent_Pic_Weixin requestMessage)
{
var responseMessage = base.CreateResponseMessage<ResponseMessageText>();
responseMessage.Content = "事件之弹出微信相册发图器";
return responseMessage;
}
/// <summary>
/// 事件之弹出地理位置选择器(location_select)
/// </summary>
/// <param name="requestMessage"></param>
/// <returns></returns>
public override IResponseMessageBase OnEvent_LocationSelectRequest(RequestMessageEvent_Location_Select requestMessage)
{
var responseMessage = base.CreateResponseMessage<ResponseMessageText>();
responseMessage.Content = "事件之弹出地理位置选择器";
return responseMessage;
}
/// <summary>
/// 事件之发送模板消息返回结果
/// </summary>
/// <param name="requestMessage"></param>
/// <returns></returns>
public override IResponseMessageBase OnEvent_TemplateSendJobFinishRequest(RequestMessageEvent_TemplateSendJobFinish requestMessage)
{
switch (requestMessage.Status)
{
case "success":
//发送成功
break;
case "failed:user block":
//送达由于用户拒收(用户设置拒绝接收公众号消息)而失败
break;
case "failed: system failed":
//送达由于其他原因失败
break;
default:
throw new WeixinException("未知模板消息状态:" + requestMessage.Status);
}
//注意:此方法内不能再发送模板消息,否则会造成无限循环!
//无需回复文字内容
//return requestMessage
// .CreateResponseMessage<ResponseMessageNoResponse>();
return null;
}
}
}
然后就行了,~~~~