You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
DS7/DSWeb/Areas/MvcShipping/Helper/DingTalkHelper.cs

67 lines
2.2 KiB
C#

using DSWeb.Areas.Dispatch.Helper;
using DSWeb.Areas.MvcShipping.DB;
using DSWeb.Areas.MvcShipping.Job;
using log4net;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Runtime.Caching;
using System.Web;
namespace DSWeb.Areas.MvcShipping.Helper
{
public static class DingTalkHelper
{
public static readonly string DingTalkAccessTokenStoreKey = "DingTalkToken";
private static ILog logger = LogManager.GetLogger("DingTalkHelper");
public static bool SendMessage(string uid, string msg)
{
DingTalkContext dingTalkContext = new DingTalkContext();
var pAgentid = dingTalkContext.ParamSets.FirstOrDefault(p => p.PARAMNAME == "DingTalkAgentid");
if (pAgentid == null || string.IsNullOrWhiteSpace(pAgentid.PARAMVALUE))
{
throw new Exception("钉钉Agentid参数配置错误");
}
var user = dingTalkContext.Users.FirstOrDefault(u => u.GID == uid);
if (user != null && !string.IsNullOrWhiteSpace(user.DingTalkAccount))
{
var token = MemoryCache.Default[DingTalkTokenJob.DingTalkAccessTokenStoreKey].ToString();
var json = new
{
agent_id = pAgentid.PARAMVALUE,
userid_list = user.DingTalkAccount,
msg = new
{
msgtype = "text",
text = new
{
content = msg
}
}
};
string resp = WebRequestHelper.DoPost($"https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2?access_token={token}", JsonConvert.SerializeObject(json));
logger.Debug(resp);
var jsonObj = JsonConvert.DeserializeAnonymousType(resp, new
{
errcode = 0,
errmsg = "",
task_id = ""
});
if (jsonObj.errcode == 0)
{
return true;
}
}
return false;
}
}
}