企业微信官方文档:群机器人配置说明 - 企业微信API
主要问题描述:markdown -content显示和自己postman发送的不一致,后来自己手动拼凑的就可以,发现是每个内容都需要换行\n
代码实例如下:
public static string getContent(List<KitonWarning> data, List<GroupProjectDetail> pro, string title, List<string> SendType)
{
try
{
string titlemsg = "# <font color=\\\"info\\\">【" + title + "】</font> 信息发布系统报警提醒\n";
string clientstate = "";
string projectstate = "";
if (SendType.Any(a => a == "1"))//离线
{
List<KitonWarning> list = data.Where(a => a.State != 1).ToList();
if (list.Count > 0)
{
clientstate += " > ## 离线清单(<font color=\\\"warning\\\">" + list.Count + "台</font>)\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<GroupProjectDetail> list = pro.Where(a => a.TodayProject == 0 || a.NextdayProject == 0).ToList();
if (list.Count > 0)
{
projectstate = " > ## 节目单异常清单(<font color=\\\"warning\\\">" + list.Count + "个</font>)\n";
foreach (GroupProjectDetail item in list)
{
projectstate += " > " + item.GroupName + ", 今日节目单 " + (item.TodayProject == 0 ? "<font color=\\\"warning\\\">无</font>" : "<font color=\\\"comment\\\">有</font>") + " 次日节目单 " + (item.NextdayProject == 0 ? " <font color=\\\"warning\\\">无</font>" : "<font color=\\\"comment\\\">有</font>")+ "\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<RobotResult>(qyresult);
if (qy.errcode == 0)
{
HDLogManager.LogEntity.Info("机器人推送【" + item.GroupName + "】成功");
}
else
{
HDLogManager.LogEntity.Error("机器人推送【" + item.GroupName + "】失败--" + qy.errmsg);
}
}