使用.net 操作 微信公众平台 —— 回复用户消息 —— 回复图文消息

目录

  1. 使用.net 操作 微信公众平台 —— 接入

  2. 使用.net 操作 微信公众平台 —— 生成微信菜单

  3. 使用.net 操作 微信公众平台 —— 接收并回复用户消息

    3.1 使用.net 操作 微信公众平台 —— 接收用户操作 —— 详细解析

        3.1.1 使用.net 操作 微信公众平台 —— 接收用户操作 —— 关注/取消关注 公众号

        3.1.2 使用.net 操作 微信公众平台 —— 接收用户操作 —— 接收用户发送的消息

    3.2 使用.net 操作 微信公众平台 —— 回复用户消息

        3.2.1 使用.net 操作 微信公众平台 —— 回复用户消息 —— 回复文本消息

        3.2.2 使用.net 操作 微信公众平台 —— 回复用户消息 —— 回复图片消息

        3.2.3 使用.net 操作 微信公众平台 —— 回复用户消息 —— 回复图文消息

  4. 使用.net 操作 微信公众平台 —— 第三方登录


 工具


回复图文消息

<xml>
    <ToUserName>< ![CDATA[toUser]] ></ToUserName>
    <FromUserName>< ![CDATA[fromUser]] ></FromUserName>
    <CreateTime>12345678</CreateTime>
    <MsgType>< ![CDATA[news]] ></MsgType>
    <ArticleCount> 2 </ArticleCount>
    <Articles>
        <item>
            <Title>< ![CDATA[title1]] ></Title> 
            <Description>< ![CDATA[description1]] ></Description>
            <PicUrl>< ![CDATA[picurl1]] ></PicUrl>
            <Url>< ![CDATA[url1]] ></Url>
        </item>

        <item>
            <Title>< ![CDATA[title2]] ></Title> 
            <Description>< ![CDATA[description2]] ></Description>
            <PicUrl>< ![CDATA[picurl2]] ></PicUrl>
            <Url>< ![CDATA[url2]] ></Url>
        </item>
    </Articles>
</xml>
参数是否必须描述
ToUserName接收方帐号(收到的OpenID)
FromUserName开发者微信号
CreateTime消息创建时间 (整型)
MsgTypenews
ArticleCount图文消息个数,限制为10条以内
Articles多条图文消息信息,默认第一个item为大图,注意,如果图文数超过10,则将会无响应
Title图文消息标题
Description图文消息描述
PicUrl图片链接,支持JPG、PNG格式,较好的效果为大图360*200,小图200*200
Url点击图文消息跳转链接

 

被动回复

/// <summary>
/// 发送图片消息 - 被动消息
/// </summary>
/// <returns></returns>
private void SendImage()
{
    int nowtime = ConvertDateTimeInt(DateTime.Now); // 查看工具 DateTime转为微信所需要的时间类型
    string resxml = "<xml>"
    resxml += "    <ToUserName><![CDATA[" + xmlMsg.FromUserName + "]]></ToUserName>"
    resxml += "    <FromUserName><![CDATA[" + xmlMsg.ToUserName + "]]></FromUserName>"
    resxml += "    <CreateTime>" + nowtime + "</CreateTime>"
    resxml += "    <MsgType><![CDATA[news]]></MsgType>"
    resxml += "    <Articles>"

    resxml += "    <item>"
    resxml += "        <Title>< ![CDATA[" + title1 + "] ]></Title>"
    resxml += "        <Description>< ![CDATA[" + description1 + "] ]></Description>"
    resxml += "        <PicUrl>< ![CDATA[" + picurl1 + "] ]></PicUrl>"
    resxml += "        <Url>< ![CDATA[" + url1 + "] ]></Url>"
    resxml += "    </item>"

    resxml += "    <item>"
    resxml += "        <Title>< ![CDATA[" + title2 + "] ]></Title>"
    resxml += "        <Description>< ![CDATA[" + description2 + "] ]></Description>"
    resxml += "        <PicUrl>< ![CDATA[" + picurl2 + "] ]></PicUrl>"
    resxml += "        <Url>< ![CDATA[" + url2 + "] ]></Url>"
    resxml += "    </item>"

    resxml += "    </Articles>"
    resxml += "</xml>";
    Response.Write(resxml);
    Response.End();
}

客服消息

/// <summary>
/// 发送图文消息 - 客服消息
/// </summary>
/// <param name="token">工具 生成AccessToken</param>
/// <returns></returns>
private void SendImageCase(string token)
{
    string url = string.Format("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={0}", token);

    List<newsViewModel> model = new List<newsViewModel>();
    model.Add(new newsViewModel
    {
        title = "标题1",
        description = "描述1",
        url = "地址1",
        picurl = "图片1",
    });

    model.Add(new newsViewModel
    {
        title = "标题2",
        description = "",
        url = "",
        picurl = "图片2",
    });

    JObject data = new JObject();
    data.Add("touser", xmlMsg.FromUserName);
    data.Add("msgtype", "news");
    data.Add("news", JObject.FromObject(new
    {
        articles = model
    }));

    var res = HttpPost(data.ToString(), url);
}

/// <summary>
/// 图文消息模型
/// </summary>
public class newsViewModel
{
    /// <summary>
    /// 标题
    /// </summary>
    public string title { get; set; }

    /// <summary>
    /// 描述
    /// </summary>
    public string description { get; set; }

    /// <summary>
    /// 链接地址
    /// </summary>
    public string url { get; set; }

    /// <summary>
    /// 图片地址
    /// </summary>
    public string picurl { get; set; }
}

/// <summary>
/// 远程获取数据(POST)
/// </summary>
/// <param name="param"></param>
/// <param name="url"></param>
/// <returns></returns>
private dynamic HttpPost(string param, string url)
{
    //转换输入参数的编码类型,获取bytep[]数组 
    byte[] byteArray = Encoding.UTF8.GetBytes(param);
    //初始化新的webRequst
    //1. 创建httpWebRequest对象
    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(url));
    //2. 初始化HttpWebRequest对象
    webRequest.Method = "POST";
    webRequest.ContentType = "application/json";
    webRequest.ContentLength = byteArray.Length;
    Stream newStream = webRequest.GetRequestStream();
    newStream.Write(byteArray, 0, byteArray.Length);
    newStream.Close();
    //4. 读取服务器的返回信息
    HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
    StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
    string srend = sr.ReadToEnd();
    sr.Close();
    response.Close();
    return srend;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值