BaseMessage.cs
1 using System;
2 using System.Net.Mime;
3 using System.Text.RegularExpressions;
5 using Newtonsoft.Json;
6 using Newtonsoft.Json.Linq;
7 
8 namespace Microsoft.Bot.Builder.Calling.ObjectModel.Contracts
9 {
21  [JsonObject(MemberSerialization = MemberSerialization.OptOut)]
22  public abstract class BaseMessage
23  {
29  private static Regex MriWeakRegex = new Regex("^\\d{1,8}\\:.{1,512}$", RegexOptions.Compiled);
30 
31  public static readonly ContentType JsonContentType = new ContentType("application/json");
32  public const string ToHeaderKey = "to";
33  public const string FromHeaderKey = "from";
34  public const string MessageTypeHeaderKey = "type";
35 
36  protected BaseMessage(string messageType)
37  {
38  Utils.AssertArgument(!string.IsNullOrWhiteSpace(messageType), "messageType is missing.");
39  this.MessageType = messageType;
40  }
41 
42  [JsonProperty(PropertyName = "type", Required = Required.Always, Order = -900)]
43  public string MessageType { get; set; }
44 
45  [JsonProperty(PropertyName = "to", Required = Required.Always, Order = -800)]
46  public string To { get; set; }
47 
48  [JsonProperty(PropertyName = "from", Required = Required.Always, Order = -700)]
49  public string From { get; set; }
50 
51  public void Validate()
52  {
53  VerifyPropertyExists(this.MessageType, "MessageType");
54  ValidateMri(this.To, "To");
55  ValidateMri(this.From, "From");
56  this.ValidateInternal();
57  }
58 
59  public static string GetMessageType(string json)
60  {
61  JObject jObj;
62  try
63  {
64  jObj = JObject.Parse(json);
65  }
66  catch (JsonReaderException)
67  {
68  return null;
69  }
70 
71  JToken typeToken = jObj[MessageTypeHeaderKey];
72  return typeToken == null ? null : typeToken.ToString();
73  }
74 
75  protected static void VerifyPropertyExists(string value, string name)
76  {
77  Utils.AssertArgument(!string.IsNullOrWhiteSpace(value), name + " is missing.");
78  }
79 
80  protected static void ValidateMri(string value, string name)
81  {
82  VerifyPropertyExists(value, name);
83  if (!MriWeakRegex.IsMatch(value))
84  {
85  throw new ArgumentException(name + " is not a valid MRI.");
86  }
87  }
88 
89  protected abstract void ValidateInternal();
90  }
91 }
Root namespace for the Microsoft Bot Builder Calling SDK object model.
static void AssertArgument(bool condition, string format, params object[] args)
Argument checker
Definition: Utils.cs:17
static void ValidateMri(string value, string name)
Definition: BaseMessage.cs:80
Root namespace for the Microsoft Bot Builder Calling SDK.
static void VerifyPropertyExists(string value, string name)
Definition: BaseMessage.cs:75
Namespace for utilities and helpers in the Microsoft Bot Builder Calling SDK.
Base message wrapper for all API messages. The format is: { type: (type-of-message), to: (to-user-MRI), from: (from-user-MRI), (optional additional-message-specific-properties), (optional additional-message-specific-properties), (optional additional-message-specific-properties), }
Definition: BaseMessage.cs:22
Root namespace for the Microsoft Bot Builder SDK.