ValidActions.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
5 
6 namespace Microsoft.Bot.Builder.Calling.ObjectModel.Contracts
7 {
11  public static class ValidActions
12  {
16  public const string AnswerAction = "answer";
17 
21  public const string AnswerAppHostedMediaAction = "answerAppHostedMedia";
22 
26  public const string HangupAction = "hangup";
27 
31  public const string PlayPromptAction = "playPrompt";
32 
36  public const string RecordAction = "record";
37 
41  public const string RecognizeAction = "recognize";
42 
46  public const string RejectAction = "reject";
47 
51  public const string PlaceCallAction = "placeCall";
52 
56  public const string VideoSubscriptionAction = "videoSubscription";
57 
61  public const string TransferAction = "transfer";
62 
68  private static Dictionary<string, int> actionsList = new Dictionary<string, int>()
69  {
70  {RejectAction, -2},
71  {AnswerAction, 1},
72  {AnswerAppHostedMediaAction, 1},
73  {PlaceCallAction, 1},
74  {VideoSubscriptionAction, 1},
75  {PlayPromptAction, 2},
76  {RecordAction, 2},
77  {RecognizeAction, 2},
78  {TransferAction, 2},
79  {HangupAction, 3},
80  };
81 
82  public static void Validate(string action)
83  {
84  Utils.AssertArgument(!String.IsNullOrWhiteSpace(action), "Action Name cannot be null or empty");
85  Utils.AssertArgument(actionsList.ContainsKey(action), "{0} is not a valid action", action);
86  }
87 
88  public static void Validate(IEnumerable<ActionBase> actions)
89  {
90  Utils.AssertArgument(actions != null, "Null Actions List not allowed");
91  ActionBase[] actionsTobeValidated = actions.ToArray();
92  Utils.AssertArgument(actionsTobeValidated.Length > 0, "Empty Actions List not allowed");
93 
94  if (actionsTobeValidated.Length > 1 && actionsTobeValidated.Any((a) => { return a.IsStandaloneAction; }))
95  {
97  false,
98  "The stand-alone action '{0}' cannot be specified with any other actions",
99  (actionsTobeValidated.FirstOrDefault((a) => { return a.IsStandaloneAction; })).Action);
100  }
101 
102  int prevOrder = 0;
103  int currOrder = 0;
104  HashSet<string> temp = new HashSet<string>();
105 
106  ActionBase actionBase = actionsTobeValidated[0];
107  Utils.AssertArgument(actionBase != null, "action cannot be null");
108  string action = actionBase.Action;
109 
110  actionBase.Validate();
111  bool condition = actionsList.TryGetValue(action, out prevOrder);
112  Utils.AssertArgument(condition, "{0} is not a valid action", action);
113  temp.Add(action);
114 
115  for (int i = 1; i < actionsTobeValidated.Length; i++)
116  {
117  actionBase = actionsTobeValidated[i];
118  Utils.AssertArgument(actionBase != null, "action cannot be null");
119  action = actionsTobeValidated[i].Action;
120  actionBase.Validate();
121  condition = actionsList.TryGetValue(action, out currOrder);
122  Utils.AssertArgument(condition, "{0} is not a valid action", action);
123  Utils.AssertArgument((currOrder >= prevOrder) && (Math.Sign(currOrder) == Math.Sign(prevOrder)),
124  "Action : {0} violates action ordering requirement",
125  action);
126  Utils.AssertArgument(!temp.Contains(action), "Action : {0} cannot be specified multiple times in the same response", action);
127 
128  temp.Add(action);
129  prevOrder = currOrder;
130  }
131 
132  Utils.AssertArgument(!(temp.Contains(AnswerAction) && temp.Contains(PlaceCallAction)), "Both Answer and PlaceCall cannot be specified");
133  }
134  }
135 }
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
This is a helper class for validating actions specified by customers
Definition: ValidActions.cs:11
static void Validate(IEnumerable< ActionBase > actions)
Definition: ValidActions.cs:88
string Action
The type of action. Various concrete action classes specify their name. This is used to deserialize a...
Definition: ActionBase.cs:40
Root namespace for the Microsoft Bot Builder Calling SDK.
Namespace for utilities and helpers in the Microsoft Bot Builder Calling SDK.
Root namespace for the Microsoft Bot Builder SDK.