哪位高手用.net做过微信?帮忙看看这段代码为什么不能实现被关注时自动回复图文消息?

爱_逆光 2014-06-14 11:30:50
哪位高手用.net做过微信?帮忙看看这段代码为什么不能实现被关注时自动回复图文消息?

//自定义一个微信消息实体类
public class wxmessage
{
public string FromUserName { get; set; }
public string ToUserName { get; set; }
public string MsgType { get; set; }
public string EventName { get; set; }
public string Content { get; set; }
public string Recognition { get; set; }
public string MediaId { get; set; }
public string EventKey { get; set; }
public string Location_X { get; set; }
public string Location_Y { get; set; }
public string Scale { get; set; }
public string Label { get; set; }
public string Latitude { get; set; }
public string Longitude { get; set; }
public string Precision { get; set; }

}
class ACCESSTOKEN
{
public string access_token { get; set; }
public string expires_in { get; set; }
}
public partial class _Default : System.Web.UI.Page
{
const string Token = "chuanxiangqixiang";//你的token
protected void Page_Load(object sender, EventArgs e)
{
//string postStr = "";
//if (Request.HttpMethod.ToLower() == "post")
//{
// System.IO.Stream s = System.Web.HttpContext.Current.Request.InputStream;
// byte[] b = new byte[s.Length];
// s.Read(b, 0, (int)s.Length);
// postStr = System.Text.Encoding.UTF8.GetString(b);
// if (!string.IsNullOrEmpty(postStr))
// {
// //ResponseMsg(postStr);
// Response.Write(ResponseMsg(postStr));
// Response.End();
// }
// //WriteLog("postStr:" + postStr);
//}
//else
//{
// Valid();
//}

MyMenu();
wxmessage wx = GetWxMessage();
WriteLog(wx.FromUserName);
string res = "";

//判断是否是关注事件。被关注事件为“subscribe”。
if (!string.IsNullOrEmpty(wx.EventName) && wx.EventName.Trim() == "subscribe")
{
//调用发送图文消息的函数sendPicTextMessage,返回string类型值给string变量res
res = sendPicTextMessage(wx, "欢迎关注川香奇香总部", "我们是川香奇香总部,欢迎各位的关注,加入我们吧!", "http://pic6.nipic.com/20100324/3793692_085815057881_2.jpg", "http://www.cxqxjm.com");
}
Response.Write(res);
}
/// <summary>
/// 返回信息结果(微信信息返回)
/// </summary>
/// <param name="weixinXML"></param>
private string ResponseMsg(string weixinXML)
{
///这里写你的返回信息代码
if (weixinXML == "你好")
{
return "你好,欢迎使用川香奇香食品研发管理有限公司公共微信平台!";
}
else
{
return "未能识别消息:" + weixinXML;
}

}

private wxmessage GetWxMessage()
{
wxmessage wx = new wxmessage();
StreamReader str = new StreamReader(Request.InputStream, System.Text.Encoding.UTF8);
XmlDocument xml = new XmlDocument();
xml.Load(str);
wx.ToUserName = xml.SelectSingleNode("xml").SelectSingleNode("ToUserName").InnerText;
wx.FromUserName = xml.SelectSingleNode("xml").SelectSingleNode("FromUserName").InnerText;
wx.MsgType = xml.SelectSingleNode("xml").SelectSingleNode("MsgType").InnerText;
WriteLog("MsgType:" + wx.MsgType);
if (wx.MsgType.Trim() == "event")
{
wx.EventName = xml.SelectSingleNode("xml").SelectSingleNode("Event").InnerText;
WriteLog(wx.EventName);
if (wx.EventName.ToUpper() == "LOCATION")
{
wx.Latitude = xml.SelectSingleNode("xml").SelectSingleNode("Latitude").InnerText;
wx.Longitude = xml.SelectSingleNode("xml").SelectSingleNode("Longitude").InnerText;
wx.Precision = xml.SelectSingleNode("xml").SelectSingleNode("Precision").InnerText;
}
else
{
wx.EventKey = xml.SelectSingleNode("xml").SelectSingleNode("EventKey").InnerText;
}
}
if (wx.MsgType.Trim() == "text")
{
wx.Content = xml.SelectSingleNode("xml").SelectSingleNode("Content").InnerText;
}
if (wx.MsgType.Trim() == "location")
{
wx.Location_X = xml.SelectSingleNode("xml").SelectSingleNode("Location_X").InnerText;
wx.Location_Y = xml.SelectSingleNode("xml").SelectSingleNode("Location_Y").InnerText;
wx.Scale = xml.SelectSingleNode("xml").SelectSingleNode("Scale").InnerText;
wx.Label = xml.SelectSingleNode("xml").SelectSingleNode("Label").InnerText;

}

if (wx.MsgType.Trim() == "voice")
{
wx.Recognition = xml.SelectSingleNode("xml").SelectSingleNode("Recognition").InnerText;
}

return wx;
}


/// <summary>
/// 发送图文消息
/// </summary>
/// <param name="_mode"></param>
/// <param name="title"></param>
/// <param name="description"></param>
/// <param name="picurl"></param>
/// <param name="url"></param>
/// <returns></returns>
private string sendPicTextMessage(wxmessage mode, string title, string description, string picurl, string url)
{

string res = string.Format(@"<xml>
<ToUserName><![CDATA[{0}]]></ToUserName>
<FromUserName><![CDATA[{1}]]></FromUserName>
<CreateTime>{2}</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<ArticleCount>1</ArticleCount>
<Articles>
<item>
<Title><![CDATA[{3}]]></Title>
<Description><![CDATA[{4}]]></Description>
<PicUrl><![CDATA[{5}]]></PicUrl>
<Url><![CDATA[{6}]]></Url>
</item>
</Articles>
</xml> ",
mode.FromUserName, mode.ToUserName, DateTime.Now, title, description, picurl, url);
return res;
}
...全文
728 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
tycthink 2016-05-25
  • 打赏
  • 举报
回复
引用 3 楼 yixian2007 的回复:
自己写个json断点测试一下。找官方的json。
我想问怎么用断点测试啊
oIWsFtw 2015-01-23
  • 打赏
  • 举报
回复

echo "hello";
echo "这个可以用微号帮-微信公众号营销助手工具设置\n所有都能回复图文​,还可以自定义图文链接,超级简单";
oIWsFtw 2015-01-22
  • 打赏
  • 举报
回复
echo “直接用微号帮-微信公众号营销助手设置,不用这么麻烦,还可以设置定时自动回复图文呢,”
sap net 2014-09-30
  • 打赏
  • 举报
回复
DateTime.Now要转换成unix的时间;时间格式不对,要转换成timestamp
  • 打赏
  • 举报
回复
照着官方的API来, 别自己随意写!
yixian2007 2014-06-17
  • 打赏
  • 举报
回复
自己定义个string变量,把上面的那个数据包放进去,就是不读取微信给你的消息,而是直接使用自己定义的消息,看看你的类的属性对不对。
yixian2007 2014-06-17
  • 打赏
  • 举报
回复
官方给的有例子的,你用官方的例子的json,就是引发事件的微信给你发送的消息,自己手写,然后在本地测试,用断点一步步地测试,看看是不是能到你的回复消息那一步,如果能到,说明微信给你的消息你是处理对了,如果不能到,说明你对微信的消息处理不正确。 再说如果再能你回复的那一步,那就说明是你返回的图文消息不对,不过我个人感觉是你在前面你处理的时候就不对了。 http://mp.weixin.qq.com/wiki/index.php?title=%E6%8E%A5%E6%94%B6%E4%BA%8B%E4%BB%B6%E6%8E%A8%E9%80%81 这是官方文档 这是xml数据包,你用这个数据包,测试是否能到你返回消息那一步。 <xml> <ToUserName><![CDATA[toUser]]></ToUserName> <FromUserName><![CDATA[FromUser]]></FromUserName> <CreateTime>123456789</CreateTime> <MsgType><![CDATA[event]]></MsgType> <Event><![CDATA[subscribe]]></Event> </xml>
爱_逆光 2014-06-16
  • 打赏
  • 举报
回复
引用 2 楼 love_love_yyou 的回复:
这个没有调用API呀,判断一个被关注事件。如果返回的话,返回到哪里呢?
json断点调试?官方Json??小弟不懂啊,您举个什么例子???? 时间没什么问题,回复纯文字消息时间显示没错啊。。。。
yixian2007 2014-06-16
  • 打赏
  • 举报
回复
另外,DateTime.Now要转换成unix的时间,单图文的方式你查看一下官方文档,感觉不大对,你这是多图文的。
yixian2007 2014-06-16
  • 打赏
  • 举报
回复
自己写个json断点测试一下。找官方的json。
爱_逆光 2014-06-14
  • 打赏
  • 举报
回复
这个没有调用API呀,判断一个被关注事件。如果返回的话,返回到哪里呢?
tcmakebest 2014-06-14
  • 打赏
  • 举报
回复
调用API时应该会返回成功或失败,仔细检查

62,067

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧