KeyboardCard.cs
1 //
2 // Copyright (c) Microsoft. All rights reserved.
3 // Licensed under the MIT license.
4 //
5 // Microsoft Bot Framework: http://botframework.com
6 //
7 // Bot Builder SDK GitHub:
8 // https://github.com/Microsoft/BotBuilder
9 //
10 // Copyright (c) Microsoft Corporation
11 // All rights reserved.
12 //
13 // MIT License:
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 //
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 //
25 // THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33 
34 using System.Collections.Generic;
35 using System.Linq;
37 using Microsoft.Bot.Connector;
38 using Newtonsoft.Json;
39 
40 namespace Microsoft.Bot.Builder.ConnectorEx
41 {
50  public partial class KeyboardCard
51  {
55  public const string ContentType = "application/vnd.microsoft.card.keyboard";
56 
62  public KeyboardCard(string text, IList<CardAction> buttons)
63  {
64  Text = text;
65  Buttons = buttons;
66  }
67 
71  [JsonProperty(PropertyName = "text")]
72  public string Text { get; set; }
73 
77  [JsonProperty(PropertyName = "buttons")]
78  public IList<CardAction> Buttons { get; set; }
79  }
80 
84  public sealed class FacebookQuickReply
85  {
86  public sealed class ContentTypes
87  {
88  public const string Text = "text";
89  public const string Location = "location";
90  }
91 
92  public FacebookQuickReply(string contentType, string title, string payload, string image = default(string))
93  {
94  ContentType = contentType;
95  Title = title;
96  Payload = payload;
97  Image = image;
98  }
99 
100  [JsonProperty(PropertyName = "content_type")]
101  public string ContentType { get; set; }
102 
103  [JsonProperty(PropertyName = "title")]
104  public string Title { get; set; }
105 
106  [JsonProperty(PropertyName = "payload")]
107  public string Payload { get; set; }
108 
109  [JsonProperty(PropertyName = "image_url")]
110  public string Image { get; set; }
111  }
112 
116  public sealed class FacebookMessage
117  {
118  public FacebookMessage(string text, IList<FacebookQuickReply> quickReplies = default(IList<FacebookQuickReply>))
119  {
120  Text = text;
121  QuickReplies = quickReplies;
122  }
123 
124  [JsonProperty(PropertyName = "text")]
125  public string Text { get; set; }
126 
127  [JsonProperty(PropertyName = "quick_replies")]
128  public IList<FacebookQuickReply> QuickReplies { get; set; }
129  }
130 
134  public static partial class KeyboardCardEx
135  {
136  public static Attachment ToAttachment(this KeyboardCard keyboard)
137  {
138  return new Attachment
139  {
140  ContentType = KeyboardCard.ContentType,
141  Content = keyboard
142  };
143  }
144 
149  public static HeroCard ToHeroCard(this KeyboardCard keyboard)
150  {
151  return new HeroCard(text: keyboard.Text, buttons: keyboard.Buttons);
152  }
153 
158  public static FacebookMessage ToFacebookMessage(this KeyboardCard keyboard)
159  {
160  return new FacebookMessage(text: keyboard.Text, quickReplies: keyboard.Buttons.Select(b => b.ToFacebookQuickReply()).ToList());
161  }
162 
163  internal static FacebookQuickReply ToFacebookQuickReply(this CardAction button)
164  {
165  return new FacebookQuickReply(contentType: FacebookQuickReply.ContentTypes.Text, title: button.Title, payload: (string)button.Value, image: button.Image);
166  }
167  }
168 }
object Value
Supplementary parameter for action. Content of this property depends on the ActionType ...
Definition: CardAction.cs:58
string Image
URL Picture which will appear on the button, next to text label.
Definition: CardAction.cs:51
Namespace for the Microsoft Bot Connector SDK.
string Title
Text description which appear on the button.
Definition: CardAction.cs:45
Extension methods for KeyboardCard
KeyboardCard(string text, IList< CardAction > buttons)
Constructs an instance of the keyboard card.
Definition: KeyboardCard.cs:62
static Attachment ToAttachment(this KeyboardCard keyboard)
static HeroCard ToHeroCard(this KeyboardCard keyboard)
Maps a KeyboardCard to a HeroCard
Facebook message format for quick reply.
const string ContentType
Content type of keyboard card for Attachment.ContentType.
Definition: KeyboardCard.cs:55
Namespace for internal Dialogs machinery that is not useful for most developers and may change in the...
static FacebookMessage ToFacebookMessage(this KeyboardCard keyboard)
Maps a KeyboardCard to a FacebookMessage
An attachment within an activity
Definition: Attachment.cs:17
FacebookMessage(string text, IList< FacebookQuickReply > quickReplies=default(IList< FacebookQuickReply >))
Facebook quick reply. See https://developers.facebook.com/docs/messenger-platform/send-api-reference/...
Definition: KeyboardCard.cs:84
IList< CardAction > Buttons
The buttons in the keyboard.
Definition: KeyboardCard.cs:78
FacebookQuickReply(string contentType, string title, string payload, string image=default(string))
Definition: KeyboardCard.cs:92
Core namespace for Dialogs and associated infrastructure.
A Hero card (card with a single, large image)
Root namespace for the Microsoft Bot Builder SDK.