In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces the example analysis of Asp.Net Core nail group robot, which has a certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article. Let's take a look at it.
Preface
Nailing, as a more and more commonly used software in enterprise office, provides interface support for internal self-research systems in order to get through the data under multi-platforms. This time, we first use the simplest nail group robot to complete various forms of message push. Refer to the custom robot link in the nail development document. This attempt does not take much time, but there are several places that need to be paid attention to.
First, set up a robot in the nail group to obtain the WebHook address
First of all, there must be a nail group. If you don't have to create one yourself, find the group robot in the menu in the upper right corner of the group and add a custom robot.
And set message push on (default is on), copy the webhook address of the next line, which will be used as the address for subsequent message push.
Just finish it. If you are not sure whether the address is valid, you can test it with a command, such as on the Linux platform, through this command and replace your own webhook_token.
Curl 'https://oapi.dingtalk.com/robot/send?access_token=cd1eb120c459ced6a65491af7b1eebbc84790fb672077a969bae8bb203aa1c52'\-H' Content-Type: application/json'\-d'{"msgtype": "text", "text": {"content": "I am who I am, a different kind of fireworks"}'
Then enter to test, and immediately receive the push message from the group of robots.
Second, refer to the documentation to complete the encapsulation of the basic class and deal with related fields
Next, to complete the call to the WebHook address in the code, you can first analyze the documents of the swarm robot, and you can learn that there are five message types: text, link, markdown (markdown), ActionCard, and FeedCard message types, in which the actionCard is divided into two categories: whole and independent. According to these types and the given parameters, the design and encapsulation of the basic class are required. Take the text type as an example:
Where msgtype is a string of five message types, so an enumeration is designed for this part as a distinction between message types.
/ enumeration of nail group robot message types / public enum MsgTypeEnum {text, link, markdown, actionCard, feedCard}
When designing a text class and giving an attribute Content, we like to use Pascal nomenclature in the design, but the nailing interface is not allowed. If we do not do some processing, directly using the Content attribute will call the impassable interface. It should be noted that when using the package provided by Newtonjson into json, replace it with the specified name to meet the nailing interface requirements.
/ public class Text {/ text content / [JsonProperty (PropertyName = "content")] public string Content {get; set;}}
Secondly, do a class encapsulation for the specified population, and you also need to deal with the replacement name of the attribute when serializing.
/ contact person / [JsonProperty (PropertyName = "atMobiles")] public List AtMobiles {set; get;} / whether @ owner / / [JsonProperty (PropertyName = "isAtAll")] public bool IsAtAll {set; get;}}
Through the analysis of five message types, some of the parameters can be shared. After a series of mining of custom nailing robot documents, these classes and enumerations are determined.
You can then finish calling the robot and pushing it to the nail group using different message types.
Third, complete the call to the nail group robot
First, the injection of HttpClientFactory is completed in the ConfigureService method.
This time, you can directly complete the call of the robot under Asp.Net Core WebApi, create a new DingTalk controller, and then complete the injection of IHttpClientFactory, and then start the next service call. For the storage of nail WebHook_Token, you can choose the configuration file or, if you just try, you can simply save it with a variable.
/ API for sending nail messages / [Route ("api/ [controller]")] [ApiController] public class DingTalkController: ControllerBase {private readonly string WebHook_Token = "https://oapi.dingtalk.com/robot/send?access_token=cd1eb120c459ced6a65491af7b1eebbc84790fb672077a969bae8bb203aa1c52"; private readonly IHttpClientFactory _ httpClientFactory; public DingTalkController (IHttpClientFactory httpClientFactory) {_ httpClientFactory = httpClientFactory;}
Once again, take the text text as an example and complete the push of the text message, establish an action to send the text message, complete the assembly of the parameters needed for the nailing interface, and finally use the unified sending method to complete the message push.
/ call the nailing robot to send text content / [HttpGet] [Route (nameof (TextContent))] public async Task TextContent () {/ / message type var msgtype = MsgTypeEnum.text.ToString (); / / text content var text = new Text {Content = "look at the red mountains all over the world. Var at = new At () {AtMobiles = new List () {"15675120617"}, IsAtAll = false}; var response = await SendDingTalkMessage (new {msgtype, text, at}); return Ok (response);}
For the sending method which needs to complete the design according to some requirements of the nailed document, such as the document indicates that the request needs to be submitted using Post and UTF8 encoding, I directly create a new method in the controller (though not very reasonable), first serialize and encapsulate the content, and then create a new client through HttpClientFactory and finish sending the message.
/ execute send message / private async Task SendDingTalkMessage (object value) {var sendMessage = JsonConvert.SerializeObject (value); var request = new HttpRequestMessage (HttpMethod.Post, WebHook_Token) {/ / nailing the document needs to specify UTF8 encoding Content = new StringContent (sendMessage, Encoding.UTF8, "application/json")}; var client = _ httpClientFactory.CreateClient (); var response = await client.SendAsync (request); return response;}
Start the program and complete the message push through the corresponding method in the url access controller. Note that the naming of each attribute or the naming after the property transformation needs to meet the nailing interface document (the last two parameters of the FeedCard type in the document are not standard).
Thank you for reading this article carefully. I hope the article "example Analysis of Asp.Net Core docking Swarm Robots" shared by the editor will be helpful to you. At the same time, I also hope you can support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.