How to Build Simple DingTalk Notification and Auto‑Reply Bots with C#
This step‑by‑step tutorial explains how to create both a notification‑type and an auto‑reply DingTalk robot, covering configuration in the DingTalk console, constructing webhook URLs, preparing JSON payloads, and implementing the required C# backend code to send and receive messages.
In this guide we walk through building a simple DingTalk robot, first covering the two main robot types—notification and auto‑reply—and then showing how to create each one.
1. Notification‑type robot
Open a DingTalk group, go to the group assistant, add a custom robot, and set the security keyword. Copy the generated Webhook URL.
Prepare a JSON payload such as:
{
"msgtype": "text",
"text": { "content": "I am a message @156xxxx8827" },
"at": { "atMobiles": ["156xxxx8827","189xxxx8325"], "isAtAll": false }
}Send the request with the following C# method:
public static string PostDingDing(string jsonString, string apiurl = null, Dictionary
headers = null)
{
if (apiurl == null) apiurl = AppSettings.WebHook;
WebRequest request = WebRequest.Create(@apiurl);
request.Method = "POST";
request.ContentType = "application/json";
if (headers != null)
{
foreach (var kv in headers)
{
if (kv.Key == "Content-Type") { request.ContentType = kv.Value; continue; }
request.Headers.Add(kv.Key, kv.Value);
}
}
if (!String.IsNullOrEmpty(jsonString))
{
byte[] bs = Encoding.UTF8.GetBytes(jsonString);
request.ContentLength = bs.Length;
using var stream = request.GetRequestStream();
stream.Write(bs, 0, bs.Length);
}
else request.ContentLength = 0;
using var response = request.GetResponse();
using var reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
return reader.ReadToEnd();
}After posting, the message appears in the group.
2. Auto‑reply robot
Configure a custom robot in the DingTalk developer console, set the server IP and a public HTTPS endpoint to receive POST messages.
Define DTO classes to deserialize incoming JSON, for example:
public class dingRotBotDto
{
public string msgtype { get; set; }
public dingText text { get; set; }
public string msgId { get; set; }
public string createAt { get; set; }
public string conversationType { get; set; }
public string conversationId { get; set; }
public string conversationTitle { get; set; }
public string senderId { get; set; }
public string senderNick { get; set; }
public string senderCorpId { get; set; }
public string senderStaffId { get; set; }
public string chatbotUserId { get; set; }
public List
atUsers { get; set; }
}
public class dingText { public string content { get; set; } }
public class dingUser { public string dingtalkId { get; set; } public string staffId { get; set; } }Implement a controller method to handle the POST:
[HttpPost("GetDingRoBot")]
public async Task
GetDingRoBot([FromBody] dingRotBotDto input)
{
var phone = await _fuluDing.GetUserPhone(input.senderStaffId);
if (input.text.content.Contains("[XXXX]"))
{
var cmd = input.text.content.Split(']')[1];
var reply = await ProcessCommandAsync(cmd);
SendMessage(reply, phone);
}
else
{
await SendMessage("Please use the correct format: [流水上账查询]XXXX", phone);
}
return Ok(ResponseResult.Execute("0", null, $"发送成功"));
}
public void SendMessage(string text, string atMobile)
{
var message = new DingDingMessage { msgtype = "text" };
message.text.content = text;
message.at.atMobiles.Add(atMobile);
var data = JsonConvert.SerializeObject(message);
_client.PostAsync("
", new StringContent(data, Encoding.UTF8, "application/json"));
}Deploy the service, enable the robot, and it will respond to messages that match the defined pattern.
Finally, click the "上线" button in the DingTalk management page to put the robot into production. Remember to include the custom keyword in every outgoing message and be aware of DingTalk's rate limit of 20 messages per minute.
Fulu Network R&D Team
Providing technical literature sharing for Fulu Holdings' tech elite, promoting its technologies through experience summaries, technology consolidation, and innovation sharing.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.