Using nails to create an enterprise internal robot

Ferro network R & D team 2021-11-25 17:57:25

Play with the nail robot

Preface
The emergence of nails saves a lot of labor costs for enterprises , It also opened up a large number of external api The interface is convenient for enterprises . Today, let's talk about the use of enterprise self-developed nail robot . Please enjoy yourself

Create a nail robot

How to create a nailing robot

Log in to nail developer background , Select application development > Enterprise internal development > robot , I'm gonna go ahead and create an app .
20211102103251
20211102103324

This is the time , The enterprise's own robot will be created .

How to call the robot api

1、 Add interface call permission . After the application is created, only the calling permission of login and message notification interface is opened by default , You need to... According to your development needs , Add the corresponding interface permission .

2、 Get applied access_token.access_token It's equivalent to an identity certificate . When calling the interface , adopt access_token To authenticate the caller's identity .

20211102103745

 Download the sdk package :
https://developers.dingtalk.com/document/app/download-the-server-side-sdk
Required for robot callback apk
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>dingtalk</artifactId>
<version>1.1.84</version>
</dependency>

Supported message formats

text type

{
"at": {
"atMobiles": [
"180xxxxxx"
],
"atUserIds": [
"user123"
],
"isAtAll": false
},
"text": {
"content": " I am me , @180xxxxxx It's a different kind of firework "
},
"msgtype": "text"
}

20211102104614

markdown type

{
"msgtype": "markdown",
"markdown": {
"title":" The weather in Hangzhou ",
"text": "#### The weather in Hangzhou @150XXXXXXXX \n> 9 degree , The northwest 1 level , The air is good 89, Relative temperature 73%\n> ![screenshot](https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png)\n> ###### 10 spot 20 Sub release [ The weather ](https://www.dingalk.com) \n"
},
"at": {
"atMobiles": [
"150XXXXXXXX"
],
"atUserIds": [
"user123"
],
"isAtAll": false
}
}

20211102104707

Jump as a whole actionCard type

{
"msgtype": "actionCard",
"actionCard": {
"title": " Build a cafe ",
"text": "![screenshot](https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png) \n #### Steve jobs 20 The apple cafe that I wanted to build years ago \n\n Apple Store The design of is moving from the original full sense of technology to life , And the trend of its life can be traced back to 20 Apple's plan to set up a coffee shop two years ago ",
"singleTitle" : " Read the whole passage ",
"singleURL" : "https://www.dingtalk.com/"
}
}

20211102104746

feedCard type

{
"msgtype": "feedCard",
"feedCard": {
"links": [
{
"title": " The train of the times is moving forward 1",
"messageURL": "https://www.dingtalk.com/",
"picURL": "https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png"
},
{
"title": " The train of the times is moving forward 2",
"messageURL": "https://www.dingtalk.com/",
"picURL": "https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png"
}
]
}
}

20211102104833

Use the nail robot to push messages

 @PostMapping(value = "/robots")
public String helloRobots(@RequestBody RobotResponse robotResponse
) throws Exception {
String[] content = robotResponse.getText().getContent().split(" ");
if (content.length == 2) {
return dingtalkRobotTypeService.findByQuestion(content[0], content[1]);
} else if (content.length == 1) {
if (content[0].equals(" Notice ")) {
return RobotReplyService.feedCard(Arrays.asList(
new RobotFeedCard.FeedCardBean.LinksBean(" The train of the times is moving forward 1", "https://www.dingtalk.com/", "https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png"),
new RobotFeedCard.FeedCardBean.LinksBean(" The train of the times is moving forward 2", "https://www.dingtalk.com/", "https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png"),
new RobotFeedCard.FeedCardBean.LinksBean(" The train of the times is moving forward 2", "https://www.dingtalk.com/", "https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png"),
new RobotFeedCard.FeedCardBean.LinksBean(" The train of the times is moving forward 2", "https://www.dingtalk.com/", "https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png")));
} else if (content[0].equals(" Activities ")) {
return RobotReplyService.aloneActionCard(" You want to participate in this activity ", "![screenshot](https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png) \n> Just say whether you want to participate in the activity or not \n>", 1, Arrays.asList(
new RobotAloneActionCard.ActionCardBean.BtnsBean(" To participate in ", "fsdfsdf"), new RobotAloneActionCard.ActionCardBean.BtnsBean(" I don't want to participate in ", " I don't want to participate in ")));
} else if (content[0].equals(" test ")) {
return RobotReplyService.markdown(null, " Cheerleading ", "[ Cheerleading ]( Cheerleading )");
}
return dingtalkRobotService.findByQuestion(content[0]);
} else {
return RobotReplyService.text(null, " I don't know what you want to say ");
}
}

Robot interactive card design

How to send interactive cards

Configure the message template

1、 Login pin OA Management backstage .

2、 Enter nailing to build a platform for interactive cards , All interactive message templates created in the current organization can be seen on the platform .
20211102105214
3、 Click new template... In the upper right corner , Then enter the template name , Select card type , Finally, click OK to complete the template creation .
20211102105538

Modify the callback address of robot interactive card

/**
* Register callback address
*
* @throws BusinessException
* @throws ApiException
*/
public static OapiImChatScencegroupInteractivecardCallbackRegisterResponse registerURL(String url) throws Exception {
DingTalkClient client = new DefaultDingTalkClient(O_API + "top/im/chat/scencegroup/interactivecard/callback/register");
RobotCallBack req = new RobotCallBack();
req.setCallbackUrl(url + "/robot/callback");
req.setApiSecret("bgRtxxxx");
req.setForceUpdate("true");
OapiImChatScencegroupInteractivecardCallbackRegisterResponse rsp = client.execute(req, AccessTokenUtil.getAccessToken());
return rsp;
}

Send robot interaction messages

/**
* Robots push interactive cards
*
* @param cardId
* @param phone
* @param cardData
* @throws Exception
*/
public static void sendCardMsg(String cardId, String phone, Map<String, String> cardData) throws Exception {
String userId = getUserIdByMobile(phone);
com.aliyun.dingtalkim_1_0.Client client = new com.aliyun.dingtalkim_1_0.Client(AccessTokenUtil.getHttpConfig());
SendInteractiveCardHeaders sendInteractiveCardHeaders = new SendInteractiveCardHeaders();
sendInteractiveCardHeaders.xAcsDingtalkAccessToken = AccessTokenUtil.getAccessToken();
SendInteractiveCardRequest sendInteractiveCardRequest = new SendInteractiveCardRequest()
.setCardTemplateId(cardId)
.setReceiverUserIdList(Collections.singletonList(userId))
.setOutTrackId(IdUtil.simpleUUID())
.setCardData(new SendInteractiveCardRequest.SendInteractiveCardRequestCardData().setCardParamMap(cardData))
.setOpenConversationId("cidJeCuP2boERqlkwy0rv4qHg")
.setConversationType(1);
try {
client.sendInteractiveCardWithOptions(sendInteractiveCardRequest, sendInteractiveCardHeaders, new RuntimeOptions());
} catch (TeaException err) {
log.error(JSON.toJSONString(err.getData()));
} catch (Exception _err) {
TeaException err = new TeaException(_err.getMessage(), _err);
log.error(JSON.toJSONString(err.getData()));
}
}

20211102105837

Accept the interactive card callback message

 /**
* The nail robot triggers a callback event
*
* @param tractId
* @param cardData
* @throws Exception
*/
public static void updateCardMsg(String tractId, Map<String, String> cardData) throws Exception {
com.aliyun.dingtalkim_1_0.Client client = new com.aliyun.dingtalkim_1_0.Client(AccessTokenUtil.getHttpConfig());
UpdateInteractiveCardHeaders updateInteractiveCardHeaders = new UpdateInteractiveCardHeaders();
updateInteractiveCardHeaders.xAcsDingtalkAccessToken = AccessTokenUtil.getAccessToken();
UpdateInteractiveCardRequest updateInteractiveCardRequest = new UpdateInteractiveCardRequest()
.setOutTrackId(tractId)
.setCardData(new UpdateInteractiveCardRequest.UpdateInteractiveCardRequestCardData()
.setCardParamMap(cardData))
.setUserIdType(1);
try {
UpdateInteractiveCardResponse updateInteractiveCardResponse = client.updateInteractiveCardWithOptions(updateInteractiveCardRequest, updateInteractiveCardHeaders, new RuntimeOptions());
log.info(JSONObject.toJSONString(updateInteractiveCardResponse));
} catch (TeaException err) {
log.error(JSON.toJSONString(err.getData()));
} catch (Exception _err) {
TeaException err = new TeaException(_err.getMessage(), _err);
log.error(JSON.toJSONString(err.getData()));
}
}

20211102110015

This is the time We can develop the corresponding business logic according to our own needs

Article reference : Nail open development center

ferro · Research and development center Fu Xiaoxiong
Please bring the original link to reprint ,thank
Similar articles

2021-11-25

2021-11-25

2021-11-25

2021-11-25

2021-11-25

2021-11-25