62,067
社区成员
发帖
与我相关
我的任务
分享
//自定义一个微信消息实体类
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;
}
echo "hello";
echo "这个可以用微号帮-微信公众号营销助手工具设置\n所有都能回复图文,还可以自定义图文链接,超级简单";
echo “直接用微号帮-微信公众号营销助手设置,不用这么麻烦,还可以设置定时自动回复图文呢,”