簡體   English   中英

機器人框架歡迎消息。 如何CardAction?

[英]bot framework welcome message. how to CardAction?

在使用ms azure botFrameWork v3。 我想顯示歡迎信息CardAction。 不是一個簡單的消息我想使用CardAction。 我不知道如何編碼。

public class MessagesController : ApiController{
            public virtual async Task<HttpResponseMessage> Post([FromBody] Activity activity){
                if (activity != null && activity.GetActivityType() == ActivityTypes.Message){
                    await Conversation.SendAsync(activity, () => new EchoDialog());
                }else{
                    HandleSystemMessage(activity);
                }
                return new HttpResponseMessage(System.Net.HttpStatusCode.Accepted);
            }

            private Activity HandleSystemMessage(Activity message){
                if (message.Type == ActivityTypes.DeleteUserData){
                }
                else if (message.Type == ActivityTypes.ConversationUpdate){
                    if (message.MembersAdded.Any(o => o.Id == message.Recipient.Id)){
                        ////////////welcom
                        var reply = message.CreateReply("hello~");
                        ConnectorClient connector = new ConnectorClient(new Uri(message.ServiceUrl));
                        connector.Conversations.ReplyToActivityAsync(reply);   
                    }
    return null;
            }
        }
    }

有多種方法可以做到這一點。 一種非常方便的方法是使用自適應卡設計器

只需設計您想要的消息,並將生成的json存儲為項目中的文件,例如在Cards/Welcome.json

當然,您可以使用代碼構建自適應卡,但這樣一來,您可以更輕松地學習自適應卡的概念。

從那里你可以在MessageController.cs做這樣的事情:

private async Task<Activity> HandleSystemMessage(Activity message)
{
    if (message.Type == ActivityTypes.ConversationUpdate)
    {
        // Handle conversation state changes, like members being added and removed
        // Use Activity.MembersAdded and Activity.MembersRemoved and Activity.Action for info
        // Not available in all channels
        if (message.MembersAdded.Any())
        {
            var reply = message.CreateReply();
            foreach (var member in message.MembersAdded)
            {
                // the bot is always added as a user of the conversation, since we don't
                // want to display the adaptive card twice ignore the conversation update triggered by the bot
                if (member.Name.ToLower() != "bot")
                {
                    // Read the welcome card from file system and send it as attachment to the user
                    string json = File.ReadAllText(HttpContext.Current.Request.MapPath("~\\Cards\\Welcome.json"));

                    AdaptiveCard card = JsonConvert.DeserializeObject<AdaptiveCard>(json);
                    reply.Attachments.Add(new Attachment
                    {
                        ContentType = AdaptiveCard.ContentType,
                        Content = card
                    });

                    using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, message))
                    {
                        var connectorClient = scope.Resolve<IConnectorClient>();
                        await connectorClient.Conversations.ReplyToActivityAsync(reply);
                    }                                
                }
            }
        }
    }

    return null;
}

請務必注意自適應卡支持您的頻道。 如果您的頻道不支持自適應卡,您可以使用英雄卡 ,然后再次回退到純文本。

個人注意事項:我發現有關各種渠道的自適應卡的狀態和支持的文檔大部分已過時,有時也是錯誤的。 如果您發現自適應卡不能在特定通道上進行渲染,則值得檢查GitHub或StackOverflow。

Bot模擬器中的結果

Bot模擬器中的歡迎消息

歡迎自適應卡樣品

{
    "type": "AdaptiveCard",
    "body": [
        {
            "type": "Container",
            "items": [
                {
                    "type": "TextBlock",
                    "size": "Large",
                    "weight": "Bolder",
                    "text": "Welcome"
                },
                {
                    "type": "ColumnSet",
                    "columns": [
                        {
                            "type": "Column",
                            "items": [
                                {
                                    "type": "TextBlock",
                                    "size": "Small",
                                    "weight": "Bolder",
                                    "text": "This is a bot",
                                    "wrap": true
                                }
                            ],
                            "width": "stretch"
                        }
                    ]
                }
            ]
        },
        {
            "type": "Container",
            "items": [
                {
                    "type": "TextBlock",
                    "text": "I'm helping you...",
                    "wrap": true
                }
            ]
        }
    ],
    "actions": [
        {
            "type": "Action.Submit",
            "title": "Quick Quote",
            "data": {
                "action": "quickquote"
            }
        },
        {
            "type": "Action.Submit",
            "title": "Some Action",
            "data": {
                "action": "someAction"
            }
        }
    ],
    "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
    "version": "1.0"
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM