使用.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 消息创建时间 (整型)
MsgType news
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;
}

猜你喜欢

转载自blog.csdn.net/qq_31267183/article/details/83818910