C# 接入企业微信群聊--机器人【markdown】

企业微信官方文档:群机器人配置说明 - 企业微信API

C# 接入企业微信群聊--机器人【markdown】_第1张图片

 主要问题描述:markdown -content显示和自己postman发送的不一致,后来自己手动拼凑的就可以,发现是每个内容都需要换行\n

代码实例如下:

public static string getContent(List data, List pro, string title, List SendType)
        {
            try
            {
                string titlemsg = "# 【" + title + "】 信息发布系统报警提醒\n";
                string clientstate = "";
                string projectstate = "";
                if (SendType.Any(a => a == "1"))//离线
                {
                    List list = data.Where(a => a.State != 1).ToList();
                    if (list.Count > 0)
                    {
                        clientstate += "         > ## 离线清单(" + list.Count + "台)\n";
                        foreach (KitonWarning item in list)
                        {
                            clientstate += "         > " + item.ClientCode + ", 最后登录时间:" + Convert.ToDateTime(item.LastLoginTime).ToString("yyyy-MM-dd HH:mm:dd")+ "\n";
                        }
                    }
                }
                if (SendType.Any(a => a == "1"))
                {
                    List list = pro.Where(a => a.TodayProject == 0 || a.NextdayProject == 0).ToList();
                    if (list.Count > 0)
                    {
                        projectstate = "         > ## 节目单异常清单(" + list.Count + "个)\n";
                        foreach (GroupProjectDetail item in list)
                        {
                            projectstate += "         > " + item.GroupName + ", 今日节目单 " + (item.TodayProject == 0 ? "" : "") + " 次日节目单 " + (item.NextdayProject == 0 ? " " : "")+ "\n";
                        }
                    }
                }
                if (clientstate == "" && projectstate == "")
                {
                    return "";
                }
                else
                {
                    return titlemsg + clientstate + projectstate;
                }
            }
            catch (Exception ex)
            {
                return "";
            }
        }
string content = QywxRobot.getContent(data, GroupProjectDetailList, item.GroupName, Monitor.MonitorContent); 
if (content == "")//无数据
{
    HDLogManager.LogEntity.Info("机器人无需推送【" + item.GroupName + "】--无报警信息");
}
else
{
    content += "         >";
    foreach (string str in towx)
    {
        content += "<@" + str + ">";
    } 
    string param = "{\"msgtype\":\"markdown\",\"markdown\":{\"content\":\"" + content + "\"}}";  
    string qyresult = HttpData.requestPost(Monitor.RobotUrl, param);//调取企业机器人
    RobotResult qy = objectConvertHelper.getobjectByJson(qyresult);
    if (qy.errcode == 0)
    {
        HDLogManager.LogEntity.Info("机器人推送【" + item.GroupName + "】成功");
    }
    else
    {
        HDLogManager.LogEntity.Error("机器人推送【" + item.GroupName + "】失败--" + qy.errmsg);
    }
}

你可能感兴趣的:(企业微信,c#)