1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Net.Http;
5 using System.Text.RegularExpressions;
6 using Newtonsoft.Json;
7 using Newtonsoft.Json.Linq;
9 namespace Microsoft.Bot.Connector
10 {
18  public partial class Activity :
19  IActivity,
28  {
32  public const string ContentType = "application/";
41  public Activity CreateReply(string text = null, string locale = null)
42  {
43  Activity reply = new Activity();
44  reply.Type = ActivityTypes.Message;
45  reply.Timestamp = DateTime.UtcNow;
46  reply.From = new ChannelAccount(id: this.Recipient.Id, name: this.Recipient.Name);
47  reply.Recipient = new ChannelAccount(id: this.From.Id, name: this.From.Name);
48  reply.ReplyToId = this.Id;
49  reply.ServiceUrl = this.ServiceUrl;
50  reply.ChannelId = this.ChannelId;
51  reply.Conversation = new ConversationAccount(isGroup: this.Conversation.IsGroup, id: this.Conversation.Id, name: this.Conversation.Name);
52  reply.Text = text ?? String.Empty;
53  reply.Locale = locale ?? this.Locale;
54  reply.Attachments = new List<Attachment>();
55  reply.Entities = new List<Entity>();
56  return reply;
57  }
62  [JsonExtensionData(ReadData = true, WriteData = true)]
63  public JObject Properties { get; set; } = new JObject();
89  public static IActivity CreatePingActivity() { return new Activity(ActivityTypes.Ping); }
109  protected bool IsActivity(string activity) { return string.Compare(this.Type?.Split('/').First(), activity, true) == 0; }
114  public IMessageActivity AsMessageActivity() { return IsActivity(ActivityTypes.Message) ? this : null; }
134  public ITypingActivity AsTypingActivity() { return IsActivity(ActivityTypes.Typing) ? this : null; }
144  public IEventActivity AsEventActivity() { return IsActivity(ActivityTypes.Event) ? this : null; }
149  public IInvokeActivity AsInvokeActivity() { return IsActivity(ActivityTypes.Invoke) ? this : null; }
158  public StateClient GetStateClient(MicrosoftAppCredentials credentials, string serviceUrl = null, params DelegatingHandler[] handlers)
159  {
160  bool useServiceUrl = (this.ChannelId == "emulator");
161  if (useServiceUrl)
162  return new StateClient(new Uri(this.ServiceUrl), credentials: credentials, handlers: handlers);
164  if (serviceUrl != null)
165  return new StateClient(new Uri(serviceUrl), credentials: credentials, handlers: handlers);
167  return new StateClient(credentials, true, handlers);
168  }
178  public StateClient GetStateClient(string microsoftAppId = null, string microsoftAppPassword = null, string serviceUrl = null, params DelegatingHandler[] handlers)
179  {
180  return GetStateClient(new MicrosoftAppCredentials(microsoftAppId, microsoftAppPassword), serviceUrl, handlers);
181  }
187  public bool HasContent()
188  {
189  if (!String.IsNullOrWhiteSpace(this.Text))
190  return true;
192  if (!String.IsNullOrWhiteSpace(this.Summary))
193  return true;
195  if (this.Attachments != null && this.Attachments.Any())
196  return true;
198  if (this.ChannelData != null)
199  return true;
201  return false;
202  }
208  public Mention[] GetMentions()
209  {
210  return this.Entities?.Where(entity => String.Compare(entity.Type, "mention", ignoreCase: true) == 0).Select(e => e.Properties.ToObject<Mention>()).ToArray() ?? new Mention[0];
211  }
218  public bool MentionsId(string id)
219  {
220  return this.GetMentions().Where(mention => mention.Mentioned.Id == id).Any();
221  }
227  public bool MentionsRecipient()
228  {
229  return this.GetMentions().Where(mention => mention.Mentioned.Id == this.Recipient.Id).Any();
230  }
236  public string RemoveRecipientMention()
237  {
238  return RemoveMentionText(this.Recipient.Id);
239  }
246  public string RemoveMentionText(string id)
247  {
248  foreach (var mention in this.GetMentions().Where(mention => mention.Mentioned.Id == id))
249  {
250  Text = Regex.Replace(Text, mention.Text, "", RegexOptions.IgnoreCase);
251  }
252  return this.Text;
253  }
260  public TypeT GetChannelData<TypeT>()
261  {
262  if (this.ChannelData == null)
263  return default(TypeT);
264  return ((JObject)this.ChannelData).ToObject<TypeT>();
265  }
271  public string GetActivityType()
272  {
273  var type = this.Type.Split('/').First();
274  return GetActivityType(type);
275  }
277  public static string GetActivityType(string type)
278  {
279  if (String.Equals(type, ActivityTypes.Message, StringComparison.OrdinalIgnoreCase))
280  return ActivityTypes.Message;
282  if (String.Equals(type, ActivityTypes.ContactRelationUpdate, StringComparison.OrdinalIgnoreCase))
285  if (String.Equals(type, ActivityTypes.ConversationUpdate, StringComparison.OrdinalIgnoreCase))
288  if (String.Equals(type, ActivityTypes.DeleteUserData, StringComparison.OrdinalIgnoreCase))
291  if (String.Equals(type, ActivityTypes.Typing, StringComparison.OrdinalIgnoreCase))
292  return ActivityTypes.Typing;
294  if (String.Equals(type, ActivityTypes.Ping, StringComparison.OrdinalIgnoreCase))
295  return ActivityTypes.Ping;
297  return $"{Char.ToLower(type[0])}{type.Substring(1)}";
298  }
299  }
300 }
DateTime Timestamp
UTC Time when message was sent (set by service)
Definition: Activity.cs:79
const string Event
Asynchronous external event
IEndOfConversationActivity AsEndOfConversationActivity()
Return an IEndOfConversationActivity mask if this is an end of conversation activity ...
Definition: ActivityEx.cs:139
const string Invoke
Synchronous request to invoke a command
const string InstallationUpdate
Bot added or removed from channel
bool HasContent()
Check if the message has content
Definition: ActivityEx.cs:187
IList< Attachment > Attachments
Definition: Activity.cs:198
static IEndOfConversationActivity CreateEndOfConversationActivity()
Create an instance of the Activity class with IEndOfConversationActivity masking
Definition: ActivityEx.cs:94
string RemoveRecipientMention()
Remove recipient mention text from Text property
Definition: ActivityEx.cs:236
Synchronous request to invoke an operation
const string ContactRelationUpdate
Bot added removed to contact list
IInstallationUpdateActivity AsInstallationUpdateActivity()
Return an IInstallationUpdateActivity mask if this is a installation update activity ...
Definition: ActivityEx.cs:124
A user has added a bot to their contact list, removed the bot from their contact list, or otherwise changed the relationship between user and bot
Channel account information for a conversation
ChannelAccount From
Sender address
Definition: Activity.cs:105
A bot was installed or removed from a channel
string ChannelId
ID of the channel where the activity was sent
Definition: Activity.cs:99
IMessageActivity AsMessageActivity()
Return an IMessageActivity mask if this is a message activity
Definition: ActivityEx.cs:114
ConversationAccount Conversation
Definition: Activity.cs:111
const string Message
Message from a user -> bot or bot -> User
static IInvokeActivity CreateInvokeActivity()
Create an instance of the Activity class with IInvokeActivity masking
Definition: ActivityEx.cs:104
string Locale
The language code of the Text field
Definition: Activity.cs:160
string RemoveMentionText(string id)
Replace any mention text for given id from Text property
Definition: ActivityEx.cs:246
static IActivity CreatePingActivity()
Create an instance of the Activity class with IActivity masking
Definition: ActivityEx.cs:89
ChannelAccount Recipient
(Outbound to bot only) Bot&#39;s address that received the message
Definition: Activity.cs:117
string Type
The type of the activity [message|contactRelationUpdate|converationUpdate|typing|endOfConversation|ev...
Definition: Activity.cs:67
IEventActivity AsEventActivity()
Return an IEventActivity mask if this is an event activity
Definition: ActivityEx.cs:144
IContactRelationUpdateActivity AsContactRelationUpdateActivity()
Return an IContactRelationUpdateActivity mask if this is a contact relation update activity ...
Definition: ActivityEx.cs:119
StateClient GetStateClient(string microsoftAppId=null, string microsoftAppPassword=null, string serviceUrl=null, params DelegatingHandler[] handlers)
Get StateClient appropriate for this activity
Definition: ActivityEx.cs:178
The Bot State REST API allows your bot to store and retrieve state associated with conversations cond...
Definition: StateClient.cs:57
const string Ping
Bounce a message off of the server without replying or changing it&#39;s state
static IEventActivity CreateEventActivity()
Create an instance of the Activity class with an IEventActivity masking
Definition: ActivityEx.cs:99
StateClient GetStateClient(MicrosoftAppCredentials credentials, string serviceUrl=null, params DelegatingHandler[] handlers)
Get StateClient appropriate for this activity
Definition: ActivityEx.cs:158
bool MentionsRecipient()
Is there a mention of Recipient.Id in the Text Property
Definition: ActivityEx.cs:227
Mention[] GetMentions()
Get mentions
Definition: ActivityEx.cs:208
The referenced conversation has been updated
static IConversationUpdateActivity CreateConversationUpdateActivity()
Create an instance of the Activity class with IConversationUpdateActivity masking ...
Definition: ActivityEx.cs:79
string GetActivityType()
Return the "major" portion of the activity
Definition: ActivityEx.cs:271
static ITypingActivity CreateTypingActivity()
Create an instance of the Activity class with ITypingActivity masking
Definition: ActivityEx.cs:84
const string DeleteUserData
Delete user data
IConversationUpdateActivity AsConversationUpdateActivity()
Return an IConversationUpdateActivity mask if this is a conversation update activity ...
Definition: ActivityEx.cs:129
string Text
Content for the message
Definition: Activity.cs:166
An Activity is the basic communication type for the Bot Framework 3.0 protocol
Definition: ActivityEx.cs:18
string ReplyToId
The original ID this message is a response to
Definition: Activity.cs:223
Asynchronous external event
static IMessageActivity CreateMessageActivity()
Create an instance of the Activity class with IMessageActivity masking
Definition: ActivityEx.cs:69
Shared properties for all activities
Definition: IActivity.cs:9
const string Typing
a user is typing
bool IsActivity(string activity)
True if the Activity is of the specified activity type
Definition: ActivityEx.cs:109
Attachments operations.
const string ConversationUpdate
This notification is sent when the conversation&#39;s properties change, for example the topic name...
bool MentionsId(string id)
Is there a mention of Id in the Text Property
Definition: ActivityEx.cs:218
static IContactRelationUpdateActivity CreateContactRelationUpdateActivity()
Create an instance of the Activity class with IContactRelationUpdateActivity masking ...
Definition: ActivityEx.cs:74
ITypingActivity AsTypingActivity()
Return an ITypingActivity mask if this is a typing activity
Definition: ActivityEx.cs:134
IList< Entity > Entities
Collection of Entity objects, each of which contains metadata about this activity. Each Entity object is typed.
Definition: Activity.cs:205
IInvokeActivity AsInvokeActivity()
Return an IInvokeActivity mask if this is an invoke activity
Definition: ActivityEx.cs:149
string ServiceUrl
Service endpoint where operations concerning the activity may be performed
Definition: Activity.cs:93
Conversation is ending, or a request to end the conversation
static string GetActivityType(string type)
Definition: ActivityEx.cs:277
Activity CreateReply(string text=null, string locale=null)
Take a message and create a reply message for it with the routing information set up to correctly rou...
Definition: ActivityEx.cs:41
const string EndOfConversation
End a conversation
Channel account information needed to route a message