前一篇文章已经说明基础的一些配置(很基础的)SDK有很多类库具体使用请参考源码
1.默认回复
在自定义MessageHandler类找到DefaultResponseMessage方法,如果没有自行重写父类方法代码如下
/// <summary>
/// 默认回复
/// </summary>
/// <param name="requestMessage"></param>
/// <returns></returns>
public override IResponseMessageBase DefaultResponseMessage(IRequestMessageBase requestMessage)
{
try
{
//获取accessToken
string accessToken = AccessTokenContainer.TryGetAccessToken(tenPayV3Helper.tenPayV3Info.AppId, tenPayV3Helper.tenPayV3Info.AppSecret);
//文字回复
string strMsg = "你问的问题我不太懂";
var responseMessageText = ResponseMessageBase.CreateFromRequestMessage<ResponseMessageText>(requestMessage);
responseMessageText.Content = strMsg;
return responseMessageText;
//图文回复 根据MaterialId获取从微信接口获取图文 MaterialId是微信图文的ID可从数据库取此处只是示例
var result = MediaApi.GetForeverNews(accessToken, MaterialId);
if (result.errcode == Senparc.Weixin.ReturnCode.请求成功)
{
LogHelper.Info("微信数据获取成功");
//图文回复
var responseMessagenews = base.CreateResponseMessage<ResponseMessageNews>();
foreach (var item in result.news_item)
{
responseMessagenews.Articles.Add(new Article() { Title = item.title, Description = item.digest, PicUrl = item.thumb_url, Url = item.url });
}
return responseMessagenews;
}
}
}
catch (Exception ex)
{
LogHelper.Info("默认回复:" + ex.Message);
}
var responseMessage = this.CreateResponseMessage<ResponseMessageTransfer_Customer_Service>();//ResponseMessageText也可以是News等其他类型
return responseMessage;
}
注意:只能有一个回复 我只是示例两种回复
2.关键词回复
在自定义MessageHandler类找到OnTextRequest方法,如果没有自行重写父类方法代码如下
/// <summary>
/// 处理文字请求
/// </summary>
/// <returns></returns>
public override IResponseMessageBase OnTextRequest(RequestMessageText requestMessage)
{
//获取用户发来的文字requestMessage.Content
//文字回复
var responseMessage = base.CreateResponseMessage<ResponseMessageText>();
responseMessage.Content = "这里写你要回复的内容";
return responseMessage;
string accessToken = AccessTokenContainer.TryGetAccessToken(AppID, AppSecret);
var result = MediaApi.GetForeverNews(accessToken, MaterialId);
if (result.errcode == Senparc.Weixin.ReturnCode.请求成功)
{
LogHelper.Info("微信数据获取成功");
//图文回复
var responseMessage = base.CreateResponseMessage<ResponseMessageNews>();
foreach (var item in result.news_item)
{
responseMessage.Articles.Add(new Article() { Title = item.title, Description = item.digest, PicUrl = item.thumb_url, Url = item.url });
}
return responseMessage;
}
//没有匹配默认回复 不设置会提示公众号异常(因为没给微信服务器相应)
return DefaultResponseMessage(requestMessage);
}
举一反三还有处理图片和音频视频的方法
3.推送模板消息
在微信后台添加你要使用的模板消息,定义成类型方便调用最好放到一个类或者一个文件夹下方便维护
/// <summary>
/// 客户集团审核模板消息数据定义
/// </summary>
public class TemplateMessage_JoinApplyChainSuccess : TemplateMessageBase
{
public TemplateDataItem first { get; set; }
public TemplateDataItem keyword1 { get; set; }
public TemplateDataItem keyword2 { get; set; }
public TemplateDataItem keyword3 { get; set; }
public TemplateDataItem keyword4 { get; set; }
public TemplateDataItem remark { get; set; }
/// <summary>
/// 客户信息审核 模板消息数据定义 构造函数
/// </summary>
/// <param name="_first"></param>
/// <param name="empname">营销代表</param>
/// <param name="chainName">集团名称</param>
/// <param name="chaincode">集团代码</param>
/// <param name="sendTime">发送时间</param>
/// <param name="_remark"></param>
/// <param name="url">如果不填写不会显示有详情</param>
/// <param name="templateId"></param>
public TemplateMessage_JoinApplyChainSuccess(string _first, string empname,
string chainName, string chaincode, string sendTime,
string _remark,
string url = null,
string templateId = "这里写你后台的模板ID")
: base(templateId, url, "这里是通知 即用户没点开时公众号消息的提示标题")
{
/* 模板格式 {{first.DATA}}
所属公司:{{keyword1.DATA}}
申请人:{{keyword2.DATA}}
申请时间:{{keyword3.DATA}}
{{remark.DATA}} */
first = new TemplateDataItem(_first);
keyword1 = new TemplateDataItem(empname);
keyword2 = new TemplateDataItem(chainName);
//keyword3 = new TemplateDataItem(orderAmount, "#ff0000");//显示为红色
keyword3 = new TemplateDataItem(chaincode);
keyword4 = new TemplateDataItem(sendTime);
remark = new TemplateDataItem(_remark);
}
}
调用
Task.Factory.StartNew(async () => {
//根据构造函数传递值
var data = new TemplateMessage_JoinApplyChainSuccess("您好,客户已提交公司信息请尽快审核", this.GetUser().EmployeeName, customer.BrandName, customer.BrandCode, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), "请点击详情查看信息并作出审核", string.Format("{0}/{1}/{2}?id={3}", MxConfig.AppUrl, "Customer", "ApplyChainToBeAudited", id));
var result = await TemplateApi.SendTemplateMessageAsync(MxConfig.AppId, "要发送的用户的Openid", data);
}
另外要在程序里使用appid不使用access_token可以像我这样写
/// <summary>
/// 微信参数
/// </summary>
public static class MxConfig
{
static MxConfig()
{
AccessTokenContainer.Register(MxConfig.AppId, MxConfig.AppSecret);
}
public static readonly string Token = WebConfigurationManager.AppSettings["wx_token"];//与微信公众账号后台的Token设置保持一致,区分大小写。
public static readonly string EncodingAESKey = WebConfigurationManager.AppSettings["wx_aeskey"];//与微信公众账号后台的EncodingAESKey设置保持一致,区分大小写。
public static readonly string AppId = WebConfigurationManager.AppSettings["wx_app_id"];//与微信公众账号后台的AppId设置保持一致,区分大小写。
public static readonly string AppSecret = WebConfigurationManager.AppSettings["wx_app_secret"];//微信公众号secret 。
}
定义静态类 构造函数注册AppID就可以了