調用釘釘接口發送消息


using DingTalk.Api;
using DingTalk.Api.Request;
using DingTalk.Api.Response;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{

//獲取到AccessToken獲取AccessToken需要Appkey和Appsecret,請求是GTE
DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
OapiGettokenRequest request = new OapiGettokenRequest();
request.Appkey = "dingntkbvkvtjsbpe4di";//Appkey
request.Appsecret = "r1sW4aPW5BDY4OQQ4W7LFD5CBr-uflByXHhtXna536Xit3CobhoIU7qsEG4_hWh9";//Appsecret
request.SetHttpMethod("GET");
OapiGettokenResponse response = client.Execute(request);
//獲取到AccessToken
string AccessToken = response.AccessToken;
//根據部門獲取到Urid
DefaultDingTalkClient clie = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/getDeptMember");
OapiUserGetDeptMemberRequest req = new OapiUserGetDeptMemberRequest();
req.DeptId = "1";
req.SetHttpMethod("GET");
OapiUserGetDeptMemberResponse rsp = clie.Execute(req, AccessToken);
List<string> userid= rsp.UserIds;
//獲取到Urid就是在公司里要發送到那個人的id
string Urid = userid[0];
//發送消息
IDingTalkClient cl = new DefaultDingTalkClient("https://eco.taobao.com/router/rest");
CorpMessageCorpconversationAsyncsendRequest req1 = new CorpMessageCorpconversationAsyncsendRequest();
req1.Msgtype = "oa";//發送消息是以oa的形式發送的,其他的還有text,image等形式
req1.AgentId = 283587777;//微應用ID
req1.UseridList = Urid;//收信息的userId,這個是by公司來區分,在該公司內這是一個唯一標識符
req1.ToAllUser = false;//是否發給所有人
//消息文本
req1.Msgcontent = "{\"message_url\": \"http://dingtalk.com\",\"head\": {\"bgcolor\": \"FFBBBBBB\",\"text\": \"頭部標題\"},\"body\": {\"title\": \"正文標題\",\"form\": [{\"key\": \"姓名:\", \"value\": \"張三\" },{\"key\": \"年齡:\", \"value\": \"20\" },{\"key\": \"身高:\", \"value\": \"1.8米\"},{\"key\": \"體重:\",\"value\": \"130斤\"},{\"key\": \"學歷:\",\"value\": \"本科\"},{\"key\": \"愛好:\",\"value\": \"打球、聽音樂\"}],\"rich\": {\"num\": \"15.6\",\"unit\": \"元\"},\"content\": \"大段文本大段文本大段文本大段文本大段文本大段文本大段文本大段文本大段文本大段文本大段文本大段文本\",\"image\": \"@lADOADmaWMzazQKA\",\"file_count\": \"3\",\"author\": \"李四 \"}}";
CorpMessageCorpconversationAsyncsendResponse rsp1 = cl.Execute(req1, AccessToken);//發送消息

}
}
}

我創建的是控台應用程序

執行效果如下:

sdk:下載https://ding-doc.dingtalk.com/doc#/faquestions/vzbp02

釘釘開發文檔:https://ding-doc.dingtalk.com/doc#/serverapi2/pgoxpy 

重要的參數有:AgentId ,UseridList ,Appkey,Appsecret   

注意:獲取AccessToken需要Appkey和Appsecret   

代碼在上面

描述的不太清楚,希望對大家有幫助


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM