官方文档到目前为止对这个initiate action的说明比较简洁,由于没有一步步的截图和说明,从头到尾看一遍可能还在云里雾里。
我一步步摸索着走了一遍,发现这个initiate action的功能如此强大,不敢独享,所以写此博文:
1. 新建bot,叫TestMsgExt,manifest文件中的composeExtensions部分如下,特别之处是“fetchTask"这个新的属性
"composeExtensions": [{"botId": "89e9cdd8-f500-4696-a701-7c2323f62a86","canUpdateConfiguration": true,"commands": [{"id": "start","type": "action","title": "start","description": "start","initialRun": true,"fetchTask": true,"context": ["commandBox","compose","message"],"parameters": [{"name": "param","title": "param","description": ""}]}]}
],
2. 将此bot加入某个teams里,在菜单里就可以看到这个TestMsgExt的bot了
3. 点击后,bot就会收到如下的json
{"name": "composeExtension/fetchTask","type": "invoke","timestamp": "2019-06-17T14:32:04.956Z","localTimestamp": "2019-06-18T00:32:04.956+10:00","id": "f:1361493733941541435","channelId": "msteams","serviceUrl": "https://smba.trafficmanager.net/apac/","from": {"id": "29:1l8B9m9SOOdHTqLDgmXvSrJyHfwd2ihooa7cxgtzJ8QjQ4WFC4mA_8K2Sa7jL-xUh7g4yh8sZIiDOX6vTtoaz6w","name": "Tony Xia","aadObjectId": "56c6599d-9216-4078-a8cf-3f039d36e1fd"},"conversation": {"isGroup": true,"conversationType": "channel","tenantId": "aece5000-341d-493a-841d-f67e417f1447","id": "19:bf1cbc367561473db0c3fe762c11b508@thread.skype"},"recipient": {"id": "28:89e9cdd8-f500-4696-a701-7c2323f62a86","name": "TestMsgExt"},"entities": [{"locale": "en-US","country": "US","platform": "Windows","type": "clientInfo"}],"channelData": {"channel": {"id": "19:bf1cbc367561473db0c3fe762c11b508@thread.skype"},"team": {"id": "19:bf1cbc367561473db0c3fe762c11b508@thread.skype"},"tenant": {"id": "aece5000-341d-493a-841d-f67e417f1447"},"source": {"name": "compose"}},"value": {"commandId": "start","commandContext": "compose","context": {"theme": "default"}},"locale": "en-US"
}
4. 我的bot响应如下:
{"task": {"type": "continue","value": {"card": {"contentType": "application/vnd.microsoft.card.adaptive","content": {"body": [{"type": "TextBlock","text": "Please enter the following information:"},{"type": "TextBlock","text": "Name"},{"type": "Input.Text","spacing": "None","title": "New Input.Toggle","placeholder": "Placeholder text"},{"type": "TextBlock","text": "Date of birth"},{"type": "Input.Date","spacing": "None","title": "New Input.Toggle"}],"actions": [{"type": "Action.Submit","title": "Submit1","data": {"id": "Submit1"}},{"type": "Action.Submit","title": "Submit2","data": {"id": "Submit2"}}],"type": "AdaptiveCard","$schema": "http://adaptivecards.io/schemas/adaptive-card.json","version": "1.0"}}}}
}
Teams在收到我bot的响应后会显示如下的对话框,对话框里的内容就是我上面json里的adapative card。
5. 如果我们走官方文档中的"Compose extension result response" (https://docs.microsoft.com/en-us/microsoftteams/platform/concepts/messaging-extensions/create-extensions#compose-extension-result-response) ,bot会收到如下请求:
{"name": "composeExtension/submitAction","type": "invoke","timestamp": "2019-06-17T14:36:26.449Z","localTimestamp": "2019-06-18T00:36:26.449+10:00","id": "f:2286900813950650039","channelId": "msteams","serviceUrl": "https://smba.trafficmanager.net/apac/","from": {"id": "29:1l8B9m9SOOdHTqLDgmXvSrJyHfwd2ihooa7cxgtzJ8QjQ4WFC4mA_8K2Sa7jL-xUh7g4yh8sZIiDOX6vTtoaz6w","name": "Tony Xia","aadObjectId": "56c6599d-9216-4078-a8cf-3f039d36e1fd"},"conversation": {"isGroup": true,"conversationType": "channel","tenantId": "aece5000-341d-493a-841d-f67e417f1447","id": "19:bf1cbc367561473db0c3fe762c11b508@thread.skype"},"recipient": {"id": "28:89e9cdd8-f500-4696-a701-7c2323f62a86","name": "TestMsgExt"},"entities": [{"locale": "en-US","country": "US","platform": "Windows","type": "clientInfo"}],"channelData": {"channel": {"id": "19:bf1cbc367561473db0c3fe762c11b508@thread.skype"},"team": {"id": "19:bf1cbc367561473db0c3fe762c11b508@thread.skype"},"tenant": {"id": "aece5000-341d-493a-841d-f67e417f1447"},"source": {"name": "compose"}},"value": {"commandId": "start","commandContext": "compose","context": {"theme": "default"},"data": {"id": "Submit1","undefined": ""}},"locale": "en-US"
}
bot需要做如下响应,”preview“和”attachments“里的内容是可以根据自己的内容进行变化的,我这里使用了官方文档里的json。
{"composeExtension": {"type": "result","attachmentLayout": "list","preview": {"contentType": "application/vnd.microsoft.card.thumbnail","content": {"title": "85069: Create a cool app","images": [{"url": "https://placekitten.com/200/200"}]}},"attachments": [{"contentType": "application/vnd.microsoft.teams.card.o365connector","content": {"sections": [{"activityTitle": "[85069]: Create a cool app","activityImage": "https://placekitten.com/200/200"},{"title": "Details","facts": [{"name": "Assigned to:","value": "[Larry Brown](mailto:larryb@example.com)"},{"name": "State:","value": "Active"}]}]}}]}
}
可以看到我们的输入框里就有了一个adaptive card,我们就可以发送出去了
6. 如果我们走官方文档中的"Response with an adaptive card message sent from a bot" (https://docs.microsoft.com/en-us/microsoftteams/platform/concepts/messaging-extensions/create-extensions#respond-with-an-adaptive-card-message-sent-from-a-bot) ,bot会收到如下请求:
{"name": "composeExtension/submitAction","type": "invoke","timestamp": "2019-06-17T14:40:29.473Z","localTimestamp": "2019-06-18T00:40:29.473+10:00","id": "f:6033114440747937164","channelId": "msteams","serviceUrl": "https://smba.trafficmanager.net/apac/","from": {"id": "29:1l8B9m9SOOdHTqLDgmXvSrJyHfwd2ihooa7cxgtzJ8QjQ4WFC4mA_8K2Sa7jL-xUh7g4yh8sZIiDOX6vTtoaz6w","name": "Tony Xia","aadObjectId": "56c6599d-9216-4078-a8cf-3f039d36e1fd"},"conversation": {"isGroup": true,"conversationType": "channel","tenantId": "aece5000-341d-493a-841d-f67e417f1447","id": "19:bf1cbc367561473db0c3fe762c11b508@thread.skype"},"recipient": {"id": "28:89e9cdd8-f500-4696-a701-7c2323f62a86","name": "TestMsgExt"},"entities": [{"locale": "en-US","country": "US","platform": "Windows","type": "clientInfo"}],"channelData": {"channel": {"id": "19:bf1cbc367561473db0c3fe762c11b508@thread.skype"},"team": {"id": "19:bf1cbc367561473db0c3fe762c11b508@thread.skype"},"tenant": {"id": "aece5000-341d-493a-841d-f67e417f1447"},"source": {"name": "compose"}},"value": {"commandId": "start","commandContext": "compose","context": {"theme": "default"},"data": {"id": "Submit2","undefined": ""}},"locale": "en-US"
}
bot需要返回一个adaptive card用来给用户预览,注意这里的type是botMessagePreview,这个是新的一种type。
{"composeExtension": {"type": "botMessagePreview","activityPreview": {"type": "message","attachments": [{"contentType": "application/vnd.microsoft.card.adaptive","content": {"body": [{"type": "TextBlock","text": "Please enter the following information:"},{"type": "TextBlock","text": "Name"},{"type": "Input.Text","spacing": "None","title": "New Input.Toggle","placeholder": "Placeholder text"},{"type": "TextBlock","text": "Date of birth"},{"type": "Input.Date","spacing": "None","title": "New Input.Toggle"}],"actions": [{"type": "Action.Submit","title": "Submit1","data": {"id": "Submit1"}},{"type": "Action.Submit","title": "Submit2","data": {"id": "Submit2"}}],"type": "AdaptiveCard","$schema": "http://adaptivecards.io/schemas/adaptive-card.json","version": "1.0"}}]}}
}
然后teams就会弹出对话框让用户进行对要发送的内容进行预览,预览的内容就是上面attachment里描述的adaptive card
接下去用户就可以点击Send,bot会再次收到一个消息,bot在消息处理的时候就可以发送这个预览的信息到channel里。
新的message extension处理机制,使得message extension十分灵活,同时体验的提升很多。官方文档有点简单,感兴趣的同学可以一步步走一遍,就能感受到这个新机制的强大之处。