发送钉钉第三方工作消息通知

发送钉钉第三方工作消息通知

钉钉官方定义:https://open.dingtalk.com/document/orgapp/message-notification-overview

开发前需在钉钉开发者后台配置【第三方企业应用】

如何配置请参考:【夏风中的Young Uncle】 https://www.cnblogs.com/woshuaile/p/17514015.html

配置完成后会在钉钉工作台,会展现刚刚配置完成的第三方企业应用

同时也会得到AgentID、AppKey、AppSecret;

放一张实际发送的效果图

图片中1:包含通知标题、通知内容、通知人、通知时间;2:通知状态(状态也可调用钉钉接口进行更改);3:查看详情,可以在app或PC端打开链接,进入详情页面

主:如果和你想要的效果一致,请继续浏览。否则请搜索查看其他博文(省的浪费时间)。

1、首先把配置放到 webconfig 中

2、实现方法前,需要下载钉钉SDK,获取sdk方法在

直接打开钉钉地址进行sdk下载:https://open.dingtalk.com/document/resourcedownload/download-server-sdk?spm=ding_open_doc.document.0.0.cefe40e9Q56N9D#title-dyi-gqb-z18

下载完成后,会得到 TopSdk.dll 直接放到项目bin文件下。

3、创建ToDoSendOA.cs 类实现第三方应用消息发送方法

//引用

using DingTalk.Api; using DingTalk.Api.Request; using DingTalk.Api.Response;

public class ToDoSendOA{ //获取配置信息

private string DingTalkAgentID = ConfigurationManager.AppSettings["DingTalkAgentID"].ToString(); private string DingTalkAppKey = ConfigurationManager.AppSettings["DingTalkAppKey"].ToString(); private string DingTalkAppSecret = ConfigurationManager.AppSettings["DingTalkAppSecret"].ToString();

///

/// 推送单条任务

///

///

///

private bool SendDingTalkMessage(ToDoMessageEntityOA Msg)

{

#region 参数封装

bool result = false; //获取钉钉Userid,这里是根据登录名,获取数据库保存的钉钉Userid,(可根据实际情况进行修改),具体获取钉钉USERid,下面会进行讲解

string dingTalkuserid = GetDingTalkUserID(Msg.Receiveuserloginname);

if (string.IsNullOrEmpty(dingTalkuserid))

{

_logInfo.Info("钉钉推送获取推送用户为空:" + "Receiveuserloginname:" + Msg.Receiveuserloginname);

return result;

}

string strTitle = Msg.Title;//传入参数 通知标题 //这的MessageUrl,是为了上面图片3,点击详情进入调转到详情页

string messageUrl = HttpUtility.UrlEncode(GetdingTalkUrl(Msg, WEB_H5_URL, DingTalkLoginUrl, "messgae"));

string firstname = Msg.Firstname;//传入参数通知人

List forms = new List

{

new FormDomain{ Key="发起人:" ,Value= firstname },

new FormDomain{ Key="通知时间:" ,Value=Msg.Senddate.ToString("yyyy-MM-dd HH:mm:ss")},//传入参数:发送时间

};

var oaModel = new OaMsgModel()

{

Agentid = long.Parse(DingTalkAgentID),

MessageUrl = "dingtalk://dingtalkclient/page/link?url=" + messageUrl + "&pc_slide=" + MsgPcSlide,

PcMessageUrl = "dingtalk://dingtalkclient/page/link?url=" + messageUrl + "&pc_slide=" + MsgPcSlide,

Head = new HeadDomain()

{

Text = strTitle

},

Body = new BodyDomain()

{

Title = strTitle,

Content = Msg.Title,

Form = forms

},

Touser = dingTalkuserid,

MessageType = MessageType.Oa,

Recordstatus = Msg.Recordstatus

};

#endregion

if (GetMessagedingtalk(Msg)<=0)

{

//钉钉通知

DingTalkResponse res = SendAsync(oaModel);

if (res.Success)

{

InsertDingTalk(Msg.Entiid, Msg.Datakey, res.Data, Msg.Workflowtitle,

Msg.Senduserid, Msg.Senduserloginname, Msg.Receiveuserid,

Msg.Receiveuserloginname, Msg.Recordstatus, "Message", "", DateTime.Now, "", Msg.Msgguid.ToString());

result = true;

}

return result;

}

return true;

}}

相关文章