├── .gitignore ├── Documentation ├── BackendControlledAppModel.png └── Screenshot.png ├── LICENSE ├── README.md ├── RemoteControlBotControllerSample ├── App.config ├── Notifications │ └── NotificationSender.cs ├── Program.cs ├── Properties │ └── AssemblyInfo.cs ├── RemoteControlBotControllerSample.csproj └── packages.config ├── RemoteControlBotSample.sln └── RemoteControlBotSample ├── App_Start └── WebApiConfig.cs ├── Controllers └── MessagesController.cs ├── Dialogs └── RootDialog.cs ├── Global.asax ├── Global.asax.cs ├── GlobalMessageHandlerModule.cs ├── MessageRouting └── MessageRouterScorable.cs ├── Notifications ├── Notification.cs ├── NotificationsManager.cs └── NotificationsScorable.cs ├── Properties └── AssemblyInfo.cs ├── RemoteControlBotSample.csproj ├── Web.Debug.config ├── Web.Release.config ├── Web.config ├── default.htm └── packages.config /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | x64/ 19 | x86/ 20 | bld/ 21 | [Bb]in/ 22 | [Oo]bj/ 23 | [Ll]og/ 24 | 25 | # Visual Studio 2015 cache/options directory 26 | .vs/ 27 | # Uncomment if you have tasks that create the project's static files in wwwroot 28 | #wwwroot/ 29 | 30 | # MSTest test Results 31 | [Tt]est[Rr]esult*/ 32 | [Bb]uild[Ll]og.* 33 | 34 | # NUNIT 35 | *.VisualState.xml 36 | TestResult.xml 37 | 38 | # Build Results of an ATL Project 39 | [Dd]ebugPS/ 40 | [Rr]eleasePS/ 41 | dlldata.c 42 | 43 | # DNX 44 | project.lock.json 45 | artifacts/ 46 | 47 | *_i.c 48 | *_p.c 49 | *_i.h 50 | *.ilk 51 | *.meta 52 | *.obj 53 | *.pch 54 | *.pdb 55 | *.pgc 56 | *.pgd 57 | *.rsp 58 | *.sbr 59 | *.tlb 60 | *.tli 61 | *.tlh 62 | *.tmp 63 | *.tmp_proj 64 | *.log 65 | *.vspscc 66 | *.vssscc 67 | .builds 68 | *.pidb 69 | *.svclog 70 | *.scc 71 | 72 | # Chutzpah Test files 73 | _Chutzpah* 74 | 75 | # Visual C++ cache files 76 | ipch/ 77 | *.aps 78 | *.ncb 79 | *.opendb 80 | *.opensdf 81 | *.sdf 82 | *.cachefile 83 | *.VC.db 84 | *.VC.VC.opendb 85 | 86 | # Visual Studio profiler 87 | *.psess 88 | *.vsp 89 | *.vspx 90 | *.sap 91 | 92 | # TFS 2012 Local Workspace 93 | $tf/ 94 | 95 | # Guidance Automation Toolkit 96 | *.gpState 97 | 98 | # ReSharper is a .NET coding add-in 99 | _ReSharper*/ 100 | *.[Rr]e[Ss]harper 101 | *.DotSettings.user 102 | 103 | # JustCode is a .NET coding add-in 104 | .JustCode 105 | 106 | # TeamCity is a build add-in 107 | _TeamCity* 108 | 109 | # DotCover is a Code Coverage Tool 110 | *.dotCover 111 | 112 | # NCrunch 113 | _NCrunch_* 114 | .*crunch*.local.xml 115 | nCrunchTemp_* 116 | 117 | # MightyMoose 118 | *.mm.* 119 | AutoTest.Net/ 120 | 121 | # Web workbench (sass) 122 | .sass-cache/ 123 | 124 | # Installshield output folder 125 | [Ee]xpress/ 126 | 127 | # DocProject is a documentation generator add-in 128 | DocProject/buildhelp/ 129 | DocProject/Help/*.HxT 130 | DocProject/Help/*.HxC 131 | DocProject/Help/*.hhc 132 | DocProject/Help/*.hhk 133 | DocProject/Help/*.hhp 134 | DocProject/Help/Html2 135 | DocProject/Help/html 136 | 137 | # Click-Once directory 138 | publish/ 139 | 140 | # Publish Web Output 141 | *.[Pp]ublish.xml 142 | *.azurePubxml 143 | # TODO: Comment the next line if you want to checkin your web deploy settings 144 | # but database connection strings (with potential passwords) will be unencrypted 145 | *.pubxml 146 | *.publishproj 147 | 148 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 149 | # checkin your Azure Web App publish settings, but sensitive information contained 150 | # in these scripts will be unencrypted 151 | PublishScripts/ 152 | 153 | # NuGet Packages 154 | *.nupkg 155 | # The packages folder can be ignored because of Package Restore 156 | **/packages/* 157 | # except build/, which is used as an MSBuild target. 158 | !**/packages/build/ 159 | # Uncomment if necessary however generally it will be regenerated when needed 160 | #!**/packages/repositories.config 161 | # NuGet v3's project.json files produces more ignoreable files 162 | *.nuget.props 163 | *.nuget.targets 164 | 165 | # Microsoft Azure Build Output 166 | csx/ 167 | *.build.csdef 168 | 169 | # Microsoft Azure Emulator 170 | ecf/ 171 | rcf/ 172 | 173 | # Windows Store app package directories and files 174 | AppPackages/ 175 | BundleArtifacts/ 176 | Package.StoreAssociation.xml 177 | _pkginfo.txt 178 | 179 | # Visual Studio cache files 180 | # files ending in .cache can be ignored 181 | *.[Cc]ache 182 | # but keep track of directories ending in .cache 183 | !*.[Cc]ache/ 184 | 185 | # Others 186 | ClientBin/ 187 | ~$* 188 | *~ 189 | *.dbmdl 190 | *.dbproj.schemaview 191 | *.pfx 192 | *.publishsettings 193 | node_modules/ 194 | orleans.codegen.cs 195 | 196 | # Since there are multiple workflows, uncomment next line to ignore bower_components 197 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 198 | #bower_components/ 199 | 200 | # RIA/Silverlight projects 201 | Generated_Code/ 202 | 203 | # Backup & report files from converting an old project file 204 | # to a newer Visual Studio version. Backup files are not needed, 205 | # because we have git ;-) 206 | _UpgradeReport_Files/ 207 | Backup*/ 208 | UpgradeLog*.XML 209 | UpgradeLog*.htm 210 | 211 | # SQL Server files 212 | *.mdf 213 | *.ldf 214 | 215 | # Business Intelligence projects 216 | *.rdl.data 217 | *.bim.layout 218 | *.bim_*.settings 219 | 220 | # Microsoft Fakes 221 | FakesAssemblies/ 222 | 223 | # GhostDoc plugin setting file 224 | *.GhostDoc.xml 225 | 226 | # Node.js Tools for Visual Studio 227 | .ntvs_analysis.dat 228 | 229 | # Visual Studio 6 build log 230 | *.plg 231 | 232 | # Visual Studio 6 workspace options file 233 | *.opt 234 | 235 | # Visual Studio LightSwitch build output 236 | **/*.HTMLClient/GeneratedArtifacts 237 | **/*.DesktopClient/GeneratedArtifacts 238 | **/*.DesktopClient/ModelManifest.xml 239 | **/*.Server/GeneratedArtifacts 240 | **/*.Server/ModelManifest.xml 241 | _Pvt_Extensions 242 | 243 | # Paket dependency manager 244 | .paket/paket.exe 245 | paket-files/ 246 | 247 | # FAKE - F# Make 248 | .fake/ 249 | 250 | # JetBrains Rider 251 | .idea/ 252 | *.sln.iml 253 | 254 | # Package and web configs 255 | #*.config 256 | -------------------------------------------------------------------------------- /Documentation/BackendControlledAppModel.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tompaana/remote-control-bot-sample/fac0b77c56c9385f055cce329b6e263c8f2cef54/Documentation/BackendControlledAppModel.png -------------------------------------------------------------------------------- /Documentation/Screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tompaana/remote-control-bot-sample/fac0b77c56c9385f055cce329b6e263c8f2cef54/Documentation/Screenshot.png -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Tomi Paananen 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Remote Control Bot Sample # 2 | 3 | This sample demonstrates how to control a chatbot, built on the 4 | [Microsoft Bot Framework](https://dev.botframework.com/), utilizing backchannel 5 | messages. The covering note (a blog post) for this sample is here: 6 | [Remotely Controlled Bots](http://tomipaananen.azurewebsites.net/?p=2231). 7 | It is recommended to read that first to understand how the backchannel messages 8 | work. 9 | 10 | ![Sample in action](Documentation/Screenshot.png?raw=true) 11 | 12 | ## Running and testing ## 13 | 14 | 1. Clone or copy the repository 15 | 2. Register a bot for this sample in [the bot portal](https://dev.botframework.com/) 16 | * Unfortunately registering and publishing the bot is the easiest way to get 17 | to test drive it, because the implementation is dependent on 18 | [Direct Line](https://docs.botframework.com/en-us/restapi/directline3/) 19 | 3. Get the **app ID** and **app password** (a step in registration phase) and 20 | insert them into the [Web.config](RemoteControlBotSample/Web.config) 21 | 4. Publish the bot (and insert the newly created endpoint to the bot portal) 22 | 5. In the portal, activate Direct Line and get the first secret key 23 | 6. Insert the secret key into [Program.cs](RemoteControlBotControllerSample/Program.cs) 24 | 7. Say something to the bot so that it knows who you are (your virtual address), 25 | you can do this on any channel the bot is on or using emulator 26 | * If you don't say anything the bot won't know you and cannot notify you 27 | 8. Run the console app (RemoteControlBotControllerSample) 28 | 9. Enjoy your three notifications 29 | 30 | ## Implementation ## 31 | 32 | The implementation consists of roughly 3 different concepts: 33 | 34 | 1. Backchannel receiver (implemented in this sample by 35 | [NotificationsScorable](RemoteControlBotSample/Notifications/NotificationsScorable.cs), 36 | see `PrepareAsync` method) 37 | 2. Notification type and handler (in [Notifications](RemoteControlBotSample/Notifications) folder) 38 | 3. Message routing for delivering the notifications to (specific) users 39 | * This message routing logic is a subset of features implemented and documented 40 | in [Bot Message Routing (component) project](https://github.com/tompaana/bot-message-routing) 41 | 42 | ### How does it work? ### 43 | 44 | Let's look at two scenarios. First, when the bot receives a normal message from 45 | the user: 46 | 47 | 1. A user sends a message to the bot 48 | * The Microsoft Bot Framework translates this to an object called `Activity`, 49 | where its `Text` property will contain the message 50 | 2. The received `Activity` instance is **seemingly** passed to the root dialog in 51 | [MessagesController](RemoteControlBotSample/Controllers/MessagesController.cs) 52 | class 53 | 3. Autofac checks for registered handlers at the previous step (step 2) 54 | * It will find two: [MessageRouterScorable](RemoteControlBotSample/MessageRouting/MessageRouterScorable.cs) 55 | and [NotificationsScorable](RemoteControlBotSample/Notifications/NotificationsScorable.cs) 56 | * `MessageRouterScorable` always stores the sender and the receiver of a 57 | message, if they have not been seen before (see 58 | [Chatbots as Middlemen article](http://tomipaananen.azurewebsites.net/?p=1851) 59 | to understand why) 60 | * `NotificationsScorable` on the other hand will look for specific 61 | backchannel messages, where the `Text` property of the `Activity` will read 62 | `"notification"` and then extracts the actual notification content from 63 | another property of `Activity` named `ChannelData` 64 | * In this case no backchannel message is detected and the root dialog is 65 | invoked 66 | 4. Root dialog handles the message as it is defined to 67 | 68 | In the second scenario a backend (or other entity) sends a backchannel message 69 | to the bot according to the notifications protocol we've agreed on: 70 | 71 | 1. A notification specific backchannel message is sent to the bot 72 | 2. The received `Activity` instance is **seemingly** passed to the root dialog 73 | in `MessagesController` class - however it the dialog will never receive the 74 | `Activity` because of the `NotificationsScorable` class, and that is because... 75 | 3. Autofac happens 76 | * Now a backchannel message is detected by `NotificationScorable`, the score 77 | will exist and its value will be `1.0d` (fancy way of saying the value of 78 | action double type is exactly 1), and as a result, 79 | [NotificationsManager](RemoteControlBotSample/Notifications/NotificationsManager.cs) 80 | will be told to handle the `Activity` instance and the root dialog is never 81 | invoked 82 | 4. `NotificationsManager` uses `MessageRouterManager` to deliver the 83 | notifications to desired users 84 | 85 | ### Why Autofac? ### 86 | 87 | As you noticed from the descriptions of the previous scenarios, 88 | [Autofac](https://autofac.org/) - an implementation of inversion of control 89 | (IoC) container - makes it harder to understand the execution flow of the code 90 | without a thorough inspection (or proper documentation). In other words, IoC 91 | makes it more difficult to have self-documenting code. That's why I don't like 92 | Autofac or IoC containers in general. I'd rather have the backchannel detection 93 | (and other things) written in 94 | [MessagesController class](/RemoteControlBotSample/Controllers/MessagesController.cs), 95 | before the decision to forward the `Activity` instance to the root dialog is made: 96 | 97 | ```cs 98 | WebApiConfig.MessageRouterManager.MakeSurePartiesAreTracked(activity); 99 | string notificationData = string.Empty; 100 | 101 | if (NotificationsManager.TryGetNotificationData(activity, out notificationData)) 102 | { 103 | // A notification related backchannel message was detected 104 | await NotificationsManager.SendNotificationAsync(notificationData); 105 | } 106 | else 107 | { 108 | await Conversation.SendAsync(activity, () => new RootDialog()); 109 | } 110 | ``` 111 | 112 | However, if you are using the Microsoft Bot Builder SDK, there is no escape from 113 | Autofac since the SDK is built using it. You can still avoid the use in the code 114 | you write. But that's my preference. You will have yours and it's neither right 115 | nor wrong. Only debatable :) 116 | 117 | ## See also ## 118 | 119 | * [Remotely Controlled Bots article](http://tomipaananen.azurewebsites.net/?p=2231) 120 | * [Interruption Bot Sample](https://github.com/svandenhoven/Bots/tree/master/BotNotificationInteruptions): *"Contains a bot that can handle interuptions from a proactive bot. Low Prio interruptions are stacked and only shown when a defined dialog has stopped."* 121 | * [Bot Message Routing (component) project](https://github.com/tompaana/bot-message-routing) 122 | * [Intermediator Bot Sample](https://github.com/tompaana/intermediator-bot-sample) 123 | -------------------------------------------------------------------------------- /RemoteControlBotControllerSample/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /RemoteControlBotControllerSample/Notifications/NotificationSender.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.Bot.Connector.DirectLine; 2 | using RemoteControlBotSample.Notifications; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Threading.Tasks; 6 | using Underscore.Bot.Models; 7 | 8 | namespace RemoteControlBotControllerSample.Notifications 9 | { 10 | public class NotificationSender 11 | { 12 | private const string SenderId = "RemoteControlBotControllerSample"; 13 | private const string NotificationsBackchannelId = "notification"; 14 | private const string ActivityTypeMessage = "message"; 15 | private Conversation _conversation; 16 | private string _watermark; 17 | private string _botSecret; 18 | 19 | /// 20 | /// Constructor. 21 | /// 22 | /// The bot secret (Direct Line). 23 | public NotificationSender(string botSecret) 24 | { 25 | _botSecret = botSecret; 26 | } 27 | 28 | /// 29 | /// Sends a notification to the given users with the given message. 30 | /// 31 | /// The list of users to notify. 32 | /// The notification message. 33 | /// 34 | public async Task NotifyAsync(IList partiesToNotify, string message) 35 | { 36 | if (string.IsNullOrEmpty(message)) 37 | { 38 | throw new ArgumentNullException($"The message ({nameof(message)}) is null or empty"); 39 | } 40 | 41 | Notification notification = new Notification() 42 | { 43 | Message = message 44 | }; 45 | 46 | if (partiesToNotify != null && partiesToNotify.Count > 0) 47 | { 48 | notification.PartiesToNotify.AddRange(partiesToNotify); 49 | } 50 | 51 | return await PostActivityAsync(CreateNotificationActivity(notification, NotificationsBackchannelId)); 52 | } 53 | 54 | /// 55 | /// Gets the latest reply (Activity) from the bot. 56 | /// 57 | /// The latest Activity instance or null, if none available. 58 | public async Task GetLatestReplyAsync() 59 | { 60 | ActivitySet activitySet = await GetActivitySetAsync(); 61 | Activity activity = null; 62 | 63 | if (activitySet != null 64 | && activitySet.Activities != null 65 | && activitySet.Activities.Count > 0) 66 | { 67 | #if DEBUG 68 | for (int i = 0; i < activitySet.Activities.Count; ++i) 69 | { 70 | Activity a = activitySet.Activities[i]; 71 | 72 | if (a != null) 73 | { 74 | System.Diagnostics.Debug.WriteLine($"GetLatestReplyAsync: Activity {(i + 1)}: Text: \"{a.Text}\" ChannelData: \"{a.ChannelData}\""); 75 | } 76 | } 77 | #endif 78 | activity = activitySet.Activities[activitySet.Activities.Count - 1]; 79 | } 80 | 81 | return activity; 82 | } 83 | 84 | /// 85 | /// Creates an activity containing the notification. 86 | /// 87 | /// The notification. 88 | /// The backchannel ID. 89 | /// The newly created activity. 90 | private Activity CreateNotificationActivity(Notification notification, string backchannelId) 91 | { 92 | return new Activity() 93 | { 94 | Type = ActivityTypeMessage, 95 | From = new ChannelAccount(SenderId), 96 | Text = backchannelId, 97 | ChannelData = notification.ToJsonString() 98 | }; 99 | } 100 | 101 | /// 102 | /// Posts the given activity to the bot using Direct Line client. 103 | /// 104 | /// The activity to send. 105 | /// The resoure response. 106 | private async Task PostActivityAsync(Activity activity) 107 | { 108 | ResourceResponse resourceResponse = null; 109 | 110 | using (DirectLineClient directLineClient = new DirectLineClient(_botSecret)) 111 | { 112 | if (_conversation == null) 113 | { 114 | _conversation = directLineClient.Conversations.StartConversation(); 115 | } 116 | else 117 | { 118 | directLineClient.Conversations.ReconnectToConversation(_conversation.ConversationId); 119 | } 120 | 121 | resourceResponse = await directLineClient.Conversations.PostActivityAsync(_conversation.ConversationId, activity); 122 | } 123 | 124 | return resourceResponse; 125 | } 126 | 127 | /// 128 | /// Gets the latest activity set of the current conversation. 129 | /// 130 | /// The latest activity set or null, if no conversation or no activities since the last time we checked. 131 | private async Task GetActivitySetAsync() 132 | { 133 | ActivitySet activitySet = null; 134 | 135 | if (_conversation != null) 136 | { 137 | using (DirectLineClient directLineClient = new DirectLineClient(_botSecret)) 138 | { 139 | directLineClient.Conversations.ReconnectToConversation(_conversation.ConversationId); 140 | 141 | if (string.IsNullOrEmpty(_watermark)) 142 | { 143 | activitySet = await directLineClient.Conversations.GetActivitiesAsync(_conversation.ConversationId); 144 | } 145 | else 146 | { 147 | activitySet = await directLineClient.Conversations.GetActivitiesAsync(_conversation.ConversationId, _watermark); 148 | } 149 | } 150 | 151 | if (activitySet != null) 152 | { 153 | _watermark = activitySet.Watermark; 154 | } 155 | } 156 | 157 | return activitySet; 158 | } 159 | } 160 | } 161 | -------------------------------------------------------------------------------- /RemoteControlBotControllerSample/Program.cs: -------------------------------------------------------------------------------- 1 | using RemoteControlBotControllerSample.Notifications; 2 | using RemoteControlBotSample.MessageRouting; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Threading; 6 | using System.Threading.Tasks; 7 | 8 | namespace RemoteControlBotControllerSample 9 | { 10 | class Program 11 | { 12 | const int NumberOfNotificationsToSend = 3; 13 | 14 | static void Main(string[] args) 15 | { 16 | MainAsync().GetAwaiter().GetResult(); 17 | } 18 | 19 | static async Task MainAsync() 20 | { 21 | NotificationSender notificationSender = new NotificationSender("INSERT YOUR DIRECT LINE SECRET HERE"); 22 | 23 | IList partiesToNotify = new List(); 24 | 25 | /* 26 | * You can set the parties to notify here. In order to do that you need to access the 27 | * database of users collected by the bot somehow. You could also send a backchannel 28 | * message to the bot asking for suitable parties. However, that is not the point of 29 | * this sample so I will leave the implementation to you. 30 | */ 31 | 32 | for (int i = 0; i < NumberOfNotificationsToSend; ++i) 33 | { 34 | Log($"Sending notification {(i + 1)}/{NumberOfNotificationsToSend}..."); 35 | 36 | Microsoft.Bot.Connector.DirectLine.ResourceResponse resourceResponse = 37 | await notificationSender.NotifyAsync(partiesToNotify, $"Notification test {(i + 1)}"); 38 | 39 | Log($"{((resourceResponse == null) ? "Received no response" : $"Received resource response with ID {resourceResponse.Id}")}"); 40 | 41 | #if DEBUG 42 | // The following will dump the activity info into Output (console) 43 | Microsoft.Bot.Connector.DirectLine.Activity activity = await notificationSender.GetLatestReplyAsync(); 44 | #endif 45 | 46 | Thread.Sleep(3000); 47 | } 48 | 49 | Log("Done"); 50 | } 51 | 52 | static void Log(string message) 53 | { 54 | Console.WriteLine(message); 55 | System.Diagnostics.Debug.WriteLine(message); 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /RemoteControlBotControllerSample/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("RemoteControlBotControllerSample")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("RemoteControlBotControllerSample")] 13 | [assembly: AssemblyCopyright("Copyright © 2017")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("cc1421f3-f0f0-407d-8c62-b7fcf547a670")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /RemoteControlBotControllerSample/RemoteControlBotControllerSample.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {CC1421F3-F0F0-407D-8C62-B7FCF547A670} 8 | Exe 9 | RemoteControlBotControllerSample 10 | RemoteControlBotControllerSample 11 | v4.6.2 12 | 512 13 | true 14 | 15 | 16 | 17 | AnyCPU 18 | true 19 | full 20 | false 21 | bin\Debug\ 22 | DEBUG;TRACE 23 | prompt 24 | 4 25 | 26 | 27 | AnyCPU 28 | pdbonly 29 | true 30 | bin\Release\ 31 | TRACE 32 | prompt 33 | 4 34 | 35 | 36 | 37 | ..\packages\Autofac.4.6.1\lib\net45\Autofac.dll 38 | 39 | 40 | ..\packages\Underscore.Bot.MessageRouting.0.9.2\lib\net46\BotMessageRouting.dll 41 | 42 | 43 | ..\packages\Chronic.Signed.0.3.2\lib\net40\Chronic.dll 44 | 45 | 46 | ..\packages\Microsoft.Azure.KeyVault.Core.2.0.4\lib\net45\Microsoft.Azure.KeyVault.Core.dll 47 | 48 | 49 | ..\packages\Microsoft.Bot.Builder.3.9.0.0\lib\net46\Microsoft.Bot.Builder.dll 50 | 51 | 52 | ..\packages\Microsoft.Bot.Builder.3.9.0.0\lib\net46\Microsoft.Bot.Builder.Autofac.dll 53 | 54 | 55 | ..\packages\Microsoft.Bot.Builder.3.9.0.0\lib\net46\Microsoft.Bot.Connector.dll 56 | 57 | 58 | ..\packages\Microsoft.Bot.Connector.DirectLine.3.0.2\lib\net45\Microsoft.Bot.Connector.DirectLine.dll 59 | 60 | 61 | ..\packages\Microsoft.Data.Edm.5.8.2\lib\net40\Microsoft.Data.Edm.dll 62 | 63 | 64 | ..\packages\Microsoft.Data.OData.5.8.2\lib\net40\Microsoft.Data.OData.dll 65 | 66 | 67 | ..\packages\Microsoft.Data.Services.Client.5.8.2\lib\net40\Microsoft.Data.Services.Client.dll 68 | 69 | 70 | ..\packages\Microsoft.IdentityModel.Logging.1.1.4\lib\net451\Microsoft.IdentityModel.Logging.dll 71 | 72 | 73 | ..\packages\Microsoft.IdentityModel.Protocol.Extensions.1.0.4.403061554\lib\net45\Microsoft.IdentityModel.Protocol.Extensions.dll 74 | 75 | 76 | ..\packages\Microsoft.IdentityModel.Tokens.5.1.4\lib\net451\Microsoft.IdentityModel.Tokens.dll 77 | 78 | 79 | ..\packages\Microsoft.Rest.ClientRuntime.2.3.9\lib\net452\Microsoft.Rest.ClientRuntime.dll 80 | 81 | 82 | ..\packages\Microsoft.WindowsAzure.ConfigurationManager.3.2.3\lib\net40\Microsoft.WindowsAzure.Configuration.dll 83 | 84 | 85 | ..\packages\WindowsAzure.Storage.8.4.0\lib\net45\Microsoft.WindowsAzure.Storage.dll 86 | 87 | 88 | ..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll 89 | 90 | 91 | 92 | 93 | ..\packages\System.IdentityModel.Tokens.Jwt.4.0.4.403061554\lib\net45\System.IdentityModel.Tokens.Jwt.dll 94 | 95 | 96 | 97 | ..\packages\Microsoft.AspNet.WebApi.Client.5.2.3\lib\net45\System.Net.Http.Formatting.dll 98 | 99 | 100 | 101 | 102 | ..\packages\System.Spatial.5.8.2\lib\net40\System.Spatial.dll 103 | 104 | 105 | ..\packages\Microsoft.AspNet.WebApi.Core.5.2.3\lib\net45\System.Web.Http.dll 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | Notifications\Notification.cs 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | -------------------------------------------------------------------------------- /RemoteControlBotControllerSample/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /RemoteControlBotSample.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.26206.0 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RemoteControlBotSample", "RemoteControlBotSample\RemoteControlBotSample.csproj", "{A8BA1066-5695-4D71-ABB4-65E5A5E0C3D4}" 7 | EndProject 8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RemoteControlBotControllerSample", "RemoteControlBotControllerSample\RemoteControlBotControllerSample.csproj", "{CC1421F3-F0F0-407D-8C62-B7FCF547A670}" 9 | EndProject 10 | Global 11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 12 | Debug|Any CPU = Debug|Any CPU 13 | Release|Any CPU = Release|Any CPU 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {A8BA1066-5695-4D71-ABB4-65E5A5E0C3D4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 17 | {A8BA1066-5695-4D71-ABB4-65E5A5E0C3D4}.Debug|Any CPU.Build.0 = Debug|Any CPU 18 | {A8BA1066-5695-4D71-ABB4-65E5A5E0C3D4}.Release|Any CPU.ActiveCfg = Release|Any CPU 19 | {A8BA1066-5695-4D71-ABB4-65E5A5E0C3D4}.Release|Any CPU.Build.0 = Release|Any CPU 20 | {CC1421F3-F0F0-407D-8C62-B7FCF547A670}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 21 | {CC1421F3-F0F0-407D-8C62-B7FCF547A670}.Debug|Any CPU.Build.0 = Debug|Any CPU 22 | {CC1421F3-F0F0-407D-8C62-B7FCF547A670}.Release|Any CPU.ActiveCfg = Release|Any CPU 23 | {CC1421F3-F0F0-407D-8C62-B7FCF547A670}.Release|Any CPU.Build.0 = Release|Any CPU 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | EndGlobal 29 | -------------------------------------------------------------------------------- /RemoteControlBotSample/App_Start/WebApiConfig.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using Newtonsoft.Json.Serialization; 3 | using System.Web.Http; 4 | using Underscore.Bot.MessageRouting; 5 | using Underscore.Bot.MessageRouting.DataStore; 6 | 7 | namespace RemoteControlBotSample 8 | { 9 | public static class WebApiConfig 10 | { 11 | public static MessageRouterManager MessageRouterManager 12 | { 13 | get; 14 | private set; 15 | } 16 | 17 | public static void Register(HttpConfiguration config) 18 | { 19 | // Json settings 20 | config.Formatters.JsonFormatter.SerializerSettings.NullValueHandling = NullValueHandling.Ignore; 21 | config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); 22 | config.Formatters.JsonFormatter.SerializerSettings.Formatting = Formatting.Indented; 23 | JsonConvert.DefaultSettings = () => new JsonSerializerSettings() 24 | { 25 | ContractResolver = new CamelCasePropertyNamesContractResolver(), 26 | Formatting = Newtonsoft.Json.Formatting.Indented, 27 | NullValueHandling = NullValueHandling.Ignore, 28 | }; 29 | 30 | // Web API configuration and services 31 | 32 | // Web API routes 33 | config.MapHttpAttributeRoutes(); 34 | 35 | config.Routes.MapHttpRoute( 36 | name: "DefaultApi", 37 | routeTemplate: "api/{controller}/{id}", 38 | defaults: new { id = RouteParameter.Optional } 39 | ); 40 | 41 | MessageRouterManager = new MessageRouterManager(new LocalRoutingDataManager()); 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /RemoteControlBotSample/Controllers/MessagesController.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.Bot.Connector; 2 | using RemoteControlBotSample.Dialogs; 3 | using System.Net; 4 | using System.Net.Http; 5 | using System.Threading.Tasks; 6 | using System.Web.Http; 7 | using Microsoft.Bot.Builder.Dialogs; 8 | 9 | namespace RemoteControlBotSample 10 | { 11 | [BotAuthentication] 12 | public class MessagesController : ApiController 13 | { 14 | /// 15 | /// POST: api/Messages 16 | /// Receive a message from a user and reply to it 17 | /// 18 | public async Task Post([FromBody]Activity activity) 19 | { 20 | if (activity.Type == ActivityTypes.Message) 21 | { 22 | // Option 1: Direct handling 23 | // If you want to go with this option, do remember to remove the registration 24 | // of the scorables in Global.asax.cs. 25 | /*WebApiConfig.MessageRouterManager.MakeSurePartiesAreTracked(activity); 26 | string notificationData = string.Empty; 27 | 28 | if (Notifications.NotificationsManager.TryGetNotificationData(activity, out notificationData)) 29 | { 30 | // A notification related backchannel message was detected 31 | await Notifications.NotificationsManager.SendNotificationAsync(notificationData); 32 | } 33 | else 34 | { 35 | await Conversation.SendAsync(activity, () => new RootDialog()); 36 | }*/ 37 | 38 | // Option 2: Using inversion of control (IoC) container 39 | // See Global.asax.cs and GlobalMessageHandlerModule.cs 40 | await Conversation.SendAsync(activity, () => new RootDialog()); 41 | } 42 | else 43 | { 44 | HandleSystemMessage(activity); 45 | } 46 | 47 | var response = Request.CreateResponse(HttpStatusCode.OK); 48 | return response; 49 | } 50 | 51 | private Activity HandleSystemMessage(Activity message) 52 | { 53 | if (message.Type == ActivityTypes.DeleteUserData) 54 | { 55 | // Implement user deletion here 56 | // If we handle user deletion, return a real message 57 | } 58 | else if (message.Type == ActivityTypes.ConversationUpdate) 59 | { 60 | // Handle conversation state changes, like members being added and removed 61 | // Use Activity.MembersAdded and Activity.MembersRemoved and Activity.Action for info 62 | // Not available in all channels 63 | } 64 | else if (message.Type == ActivityTypes.ContactRelationUpdate) 65 | { 66 | // Handle add/remove from contact lists 67 | // Activity.From + Activity.Action represent what happened 68 | } 69 | else if (message.Type == ActivityTypes.Typing) 70 | { 71 | // Handle knowing tha the user is typing 72 | } 73 | else if (message.Type == ActivityTypes.Ping) 74 | { 75 | } 76 | 77 | return null; 78 | } 79 | } 80 | } -------------------------------------------------------------------------------- /RemoteControlBotSample/Dialogs/RootDialog.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.Bot.Builder.Dialogs; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Threading.Tasks; 5 | using Microsoft.Bot.Connector; 6 | using Underscore.Bot.MessageRouting; 7 | using Underscore.Bot.Models; 8 | 9 | namespace RemoteControlBotSample.Dialogs 10 | { 11 | [Serializable] 12 | public class RootDialog : IDialog 13 | { 14 | #pragma warning disable 1998 15 | public async Task StartAsync(IDialogContext context) 16 | { 17 | context.Wait(OnMessageReceivedAsync); 18 | } 19 | #pragma warning restore 1998 20 | 21 | private async Task OnMessageReceivedAsync(IDialogContext context, IAwaitable result) 22 | { 23 | IMessageActivity messageActivity = await result; 24 | 25 | if (messageActivity != null) 26 | { 27 | await context.PostAsync($"The text part of the received activity reads \"{messageActivity.Text}\""); 28 | 29 | if (messageActivity.ChannelData != null) 30 | { 31 | // For debugging let's broadcast the received channel data content 32 | MessageRouterManager messageRouterManager = WebApiConfig.MessageRouterManager; 33 | IList userParties = messageRouterManager.RoutingDataManager.GetUserParties(); 34 | 35 | if (userParties.Count == 0) 36 | { 37 | await context.PostAsync("I've got no user parties! This should not happen!"); 38 | } 39 | else 40 | { 41 | // Broadcast the channel data content 42 | foreach (Party party in userParties) 43 | { 44 | await messageRouterManager.SendMessageToPartyByBotAsync( 45 | party, $"Received the following channel data (as string): {messageActivity.ChannelData.ToString()}"); 46 | } 47 | } 48 | } 49 | } 50 | else 51 | { 52 | await context.PostAsync("The received activity was null or not of type IMessageActivity!"); 53 | } 54 | 55 | context.Done(new object()); 56 | } 57 | } 58 | } -------------------------------------------------------------------------------- /RemoteControlBotSample/Global.asax: -------------------------------------------------------------------------------- 1 | <%@ Application Codebehind="Global.asax.cs" Inherits="RemoteControlBotSample.WebApiApplication" Language="C#" %> 2 | -------------------------------------------------------------------------------- /RemoteControlBotSample/Global.asax.cs: -------------------------------------------------------------------------------- 1 | using Autofac; 2 | using System.Web; 3 | using System.Web.Http; 4 | 5 | namespace RemoteControlBotSample 6 | { 7 | public class WebApiApplication : HttpApplication 8 | { 9 | protected void Application_Start() 10 | { 11 | ContainerBuilder containerBuilder = new ContainerBuilder(); 12 | containerBuilder.RegisterModule(); 13 | 14 | #pragma warning disable 0618 15 | containerBuilder.Update(Microsoft.Bot.Builder.Dialogs.Conversation.Container); 16 | #pragma warning restore 0618 17 | 18 | GlobalConfiguration.Configure(WebApiConfig.Register); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /RemoteControlBotSample/GlobalMessageHandlerModule.cs: -------------------------------------------------------------------------------- 1 | using Autofac; 2 | using Microsoft.Bot.Builder.Dialogs.Internals; 3 | using Microsoft.Bot.Builder.Scorables; 4 | using Microsoft.Bot.Connector; 5 | using RemoteControlBotSample.MessageRouting; 6 | using RemoteControlBotSample.Notifications; 7 | 8 | namespace RemoteControlBotSample 9 | { 10 | public class GlobalMessageHandlerModule : Module 11 | { 12 | protected override void Load(ContainerBuilder containerBuilder) 13 | { 14 | base.Load(containerBuilder); 15 | 16 | containerBuilder 17 | .Register(componentContext => new MessageRouterScorable(componentContext.Resolve())) 18 | .As>() 19 | .InstancePerLifetimeScope(); 20 | 21 | containerBuilder 22 | .Register(componentContext => new NotificationsScorable(componentContext.Resolve())) 23 | .As>() 24 | .InstancePerLifetimeScope(); 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /RemoteControlBotSample/MessageRouting/MessageRouterScorable.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.Bot.Connector; 2 | using Microsoft.Bot.Builder.Dialogs.Internals; 3 | using Microsoft.Bot.Builder.Internals.Fibers; 4 | using Microsoft.Bot.Builder.Scorables.Internals; 5 | using System.Threading; 6 | using System.Threading.Tasks; 7 | 8 | namespace RemoteControlBotSample.MessageRouting 9 | { 10 | public class MessageRouterScorable : ScorableBase 11 | { 12 | private readonly IDialogTask _dialogTask; 13 | 14 | public MessageRouterScorable(IDialogTask dialogTask) 15 | { 16 | SetField.NotNull(out _dialogTask, nameof(dialogTask), dialogTask); 17 | } 18 | 19 | protected override Task DoneAsync(IActivity activity, string state, CancellationToken cancellationToken) 20 | { 21 | return Task.CompletedTask; 22 | } 23 | 24 | protected override double GetScore(IActivity activity, string state) 25 | { 26 | return 0d; 27 | } 28 | 29 | protected override bool HasScore(IActivity activity, string state) 30 | { 31 | return false; 32 | } 33 | 34 | protected override async Task PostAsync(IActivity activity, string state, CancellationToken cancellationToken) 35 | { 36 | await _dialogTask.PollAsync(cancellationToken); 37 | } 38 | 39 | #pragma warning disable 1998 40 | protected override async Task PrepareAsync(IActivity activity, CancellationToken cancellationToken) 41 | { 42 | WebApiConfig.MessageRouterManager.MakeSurePartiesAreTracked(activity); 43 | return string.Empty; 44 | } 45 | #pragma warning restore 1998 46 | } 47 | } -------------------------------------------------------------------------------- /RemoteControlBotSample/Notifications/Notification.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using System; 3 | using System.Collections.Generic; 4 | using Underscore.Bot.Models; 5 | 6 | namespace RemoteControlBotSample.Notifications 7 | { 8 | [Serializable] 9 | public class Notification 10 | { 11 | public List PartiesToNotify 12 | { 13 | get; 14 | set; 15 | } 16 | 17 | public string Message 18 | { 19 | get; 20 | set; 21 | } 22 | 23 | public Notification() 24 | { 25 | PartiesToNotify = new List(); 26 | Message = string.Empty; 27 | } 28 | 29 | public string ToJsonString() 30 | { 31 | return JsonConvert.SerializeObject(this); 32 | } 33 | 34 | public static Notification FromJsonString(string jsonString) 35 | { 36 | return JsonConvert.DeserializeObject(jsonString); 37 | } 38 | } 39 | } -------------------------------------------------------------------------------- /RemoteControlBotSample/Notifications/NotificationsManager.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.Bot.Connector; 2 | using System.Linq; 3 | using System.Threading.Tasks; 4 | using Underscore.Bot.MessageRouting; 5 | using Underscore.Bot.Models; 6 | 7 | namespace RemoteControlBotSample.Notifications 8 | { 9 | /// 10 | /// Simple class for sending notifications to users. 11 | /// 12 | public static partial class NotificationsManager 13 | { 14 | public const string NotificationBackchannelId = "notification"; 15 | 16 | /// 17 | /// Handles sending of the given notification. 18 | /// 19 | /// The notification to send. 20 | /// 21 | public async static Task SendNotificationAsync(Notification notification) 22 | { 23 | if (notification != null) 24 | { 25 | MessageRouterManager messageRouterManager = WebApiConfig.MessageRouterManager; 26 | 27 | if (notification.PartiesToNotify == null || notification.PartiesToNotify.Count() == 0) 28 | { 29 | // No receivers given - broadcast the notification to all users the bot is aware of 30 | notification.PartiesToNotify.AddRange(messageRouterManager.RoutingDataManager.GetUserParties()); 31 | } 32 | 33 | if (string.IsNullOrEmpty(notification.Message)) 34 | { 35 | // Notification message missing - let's insert something for the sake of example 36 | notification.Message = "Hello! This is a test notification message."; 37 | } 38 | 39 | foreach (Party partyToNotify in notification.PartiesToNotify) 40 | { 41 | await messageRouterManager.SendMessageToPartyByBotAsync(partyToNotify, notification.Message); 42 | } 43 | } 44 | } 45 | 46 | /// 47 | /// For convenience. 48 | /// Handles sending of the given notification. 49 | /// 50 | /// The notification as a string. 51 | /// 52 | public async static Task SendNotificationAsync(string notificationAsJsonString) 53 | { 54 | Notification notification = Notification.FromJsonString(notificationAsJsonString); 55 | await SendNotificationAsync(notification); 56 | } 57 | 58 | /// 59 | /// Checks the given activity for a notification related backchannel message. 60 | /// 61 | /// The activity to check. 62 | /// Where the notification data (as string) is placed. 63 | /// Will be empty, if no backchannel message is detected. 64 | /// True, if a backchannel message was detected. False otherwise. 65 | public static bool TryGetNotificationData(Activity activity, out string notificationData) 66 | { 67 | if (activity != null 68 | && !string.IsNullOrEmpty(activity.Text) 69 | && activity.Text.Equals(NotificationBackchannelId) 70 | && activity.ChannelData != null) 71 | { 72 | // Backchannel message with ID matching notifications action found 73 | notificationData = activity.ChannelData.ToString(); 74 | return true; 75 | } 76 | 77 | notificationData = string.Empty; 78 | return false; 79 | } 80 | } 81 | } -------------------------------------------------------------------------------- /RemoteControlBotSample/Notifications/NotificationsScorable.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.Bot.Builder.Dialogs.Internals; 2 | using Microsoft.Bot.Builder.Internals.Fibers; 3 | using Microsoft.Bot.Builder.Scorables.Internals; 4 | using Microsoft.Bot.Connector; 5 | using System.Threading; 6 | using System.Threading.Tasks; 7 | 8 | namespace RemoteControlBotSample.Notifications 9 | { 10 | /// 11 | /// A class that integrates to the Microsoft Bot Framework and looks for notification related 12 | /// backchannel messages from incoming activities. 13 | /// 14 | public class NotificationsScorable : ScorableBase 15 | { 16 | private readonly IDialogTask _dialogTask; 17 | 18 | public NotificationsScorable(IDialogTask dialogTask) 19 | { 20 | SetField.NotNull(out _dialogTask, nameof(dialogTask), dialogTask); 21 | } 22 | 23 | protected override Task DoneAsync(IActivity activity, string state, CancellationToken cancellationToken) 24 | { 25 | return Task.CompletedTask; 26 | } 27 | 28 | /// 29 | /// Returns the score (match of the activity to types this class handles). 30 | /// 1 means we are going to consume the activity. 0 score means we don't care about 31 | /// the current activity. 32 | /// 33 | /// 34 | /// Will contain the notification data, if the activity contained it. 35 | /// An empty string otherwise. 36 | /// 37 | protected override double GetScore(IActivity activity, string state) 38 | { 39 | return (string.IsNullOrEmpty(state) ? 0d : 1d); 40 | } 41 | 42 | /// 43 | /// Tells us if we this scorable has reached a score or not. 44 | /// 45 | /// 46 | /// Will contain the notification data, if the activity contained it. 47 | /// An empty string otherwise. 48 | /// 49 | protected override bool HasScore(IActivity activity, string state) 50 | { 51 | return !string.IsNullOrEmpty(state); 52 | } 53 | 54 | /// 55 | /// Sends the notification(s). 56 | /// 57 | /// 58 | /// Should contain the notification as JSON string. 59 | /// We store it to the state in PrepareAsync method (see the return value). 60 | /// 61 | /// 62 | protected override async Task PostAsync(IActivity activity, string state, CancellationToken cancellationToken) 63 | { 64 | await NotificationsManager.SendNotificationAsync(state); 65 | await _dialogTask.PollAsync(cancellationToken); 66 | } 67 | 68 | /// 69 | /// Checks the given activity for a backchannel message ordering the bot to send out 70 | /// notifications. 71 | /// 72 | /// The received activity. 73 | /// 74 | /// The notification data as JSON string, if found. An empty string otherwise. 75 | #pragma warning disable 1998 76 | protected override async Task PrepareAsync(IActivity activity, CancellationToken cancellationToken) 77 | { 78 | string notificationData = string.Empty; 79 | NotificationsManager.TryGetNotificationData((activity as Activity), out notificationData); 80 | return notificationData; 81 | } 82 | #pragma warning restore 1998 83 | } 84 | } -------------------------------------------------------------------------------- /RemoteControlBotSample/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("RemoteControlBotSample")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("RemoteControlBotSample")] 13 | [assembly: AssemblyCopyright("Copyright © 2016")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("a8ba1066-5695-4d71-abb4-65e5a5e0c3d4")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Revision and Build Numbers 33 | // by using the '*' as shown below: 34 | [assembly: AssemblyVersion("1.0.0.0")] 35 | [assembly: AssemblyFileVersion("1.0.0.0")] 36 | -------------------------------------------------------------------------------- /RemoteControlBotSample/RemoteControlBotSample.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | 8 | 9 | 2.0 10 | {A8BA1066-5695-4D71-ABB4-65E5A5E0C3D4} 11 | {349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc} 12 | Library 13 | Properties 14 | RemoteControlBotSample 15 | Bot Application1 16 | v4.6 17 | true 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | true 30 | full 31 | false 32 | bin\ 33 | DEBUG;TRACE 34 | prompt 35 | 4 36 | 37 | 38 | pdbonly 39 | true 40 | bin\ 41 | TRACE 42 | prompt 43 | 4 44 | 45 | 46 | 47 | ..\packages\Autofac.4.6.1\lib\net45\Autofac.dll 48 | 49 | 50 | ..\packages\Underscore.Bot.MessageRouting.0.9.2\lib\net46\BotMessageRouting.dll 51 | 52 | 53 | ..\packages\Chronic.Signed.0.3.2\lib\net40\Chronic.dll 54 | 55 | 56 | ..\packages\Microsoft.Azure.KeyVault.Core.2.0.4\lib\net45\Microsoft.Azure.KeyVault.Core.dll 57 | 58 | 59 | ..\packages\Microsoft.Bot.Builder.3.9.0.0\lib\net46\Microsoft.Bot.Builder.dll 60 | 61 | 62 | ..\packages\Microsoft.Bot.Builder.3.9.0.0\lib\net46\Microsoft.Bot.Builder.Autofac.dll 63 | 64 | 65 | ..\packages\Microsoft.Bot.Builder.3.9.0.0\lib\net46\Microsoft.Bot.Connector.dll 66 | 67 | 68 | 69 | ..\packages\Microsoft.Data.Edm.5.8.2\lib\net40\Microsoft.Data.Edm.dll 70 | 71 | 72 | ..\packages\Microsoft.Data.OData.5.8.2\lib\net40\Microsoft.Data.OData.dll 73 | 74 | 75 | ..\packages\Microsoft.Data.Services.Client.5.8.2\lib\net40\Microsoft.Data.Services.Client.dll 76 | 77 | 78 | ..\packages\Microsoft.Extensions.Configuration.1.1.1\lib\netstandard1.1\Microsoft.Extensions.Configuration.dll 79 | 80 | 81 | ..\packages\Microsoft.Extensions.Configuration.Abstractions.1.1.1\lib\netstandard1.0\Microsoft.Extensions.Configuration.Abstractions.dll 82 | 83 | 84 | ..\packages\Microsoft.Extensions.Primitives.1.1.0\lib\netstandard1.0\Microsoft.Extensions.Primitives.dll 85 | 86 | 87 | ..\packages\Microsoft.IdentityModel.Logging.1.1.4\lib\net451\Microsoft.IdentityModel.Logging.dll 88 | 89 | 90 | ..\packages\Microsoft.IdentityModel.Protocol.Extensions.1.0.4.403061554\lib\net45\Microsoft.IdentityModel.Protocol.Extensions.dll 91 | 92 | 93 | ..\packages\Microsoft.IdentityModel.Tokens.5.1.4\lib\net451\Microsoft.IdentityModel.Tokens.dll 94 | 95 | 96 | ..\packages\Microsoft.Rest.ClientRuntime.2.3.9\lib\net452\Microsoft.Rest.ClientRuntime.dll 97 | 98 | 99 | ..\packages\Microsoft.Web.Infrastructure.1.0.0.0\lib\net40\Microsoft.Web.Infrastructure.dll 100 | 101 | 102 | ..\packages\Microsoft.Win32.Primitives.4.3.0\lib\net46\Microsoft.Win32.Primitives.dll 103 | 104 | 105 | ..\packages\Microsoft.WindowsAzure.ConfigurationManager.3.2.3\lib\net40\Microsoft.WindowsAzure.Configuration.dll 106 | 107 | 108 | ..\packages\WindowsAzure.Storage.8.4.0\lib\net45\Microsoft.WindowsAzure.Storage.dll 109 | 110 | 111 | ..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll 112 | 113 | 114 | ..\packages\System.AppContext.4.3.0\lib\net46\System.AppContext.dll 115 | 116 | 117 | 118 | ..\packages\System.Console.4.3.0\lib\net46\System.Console.dll 119 | 120 | 121 | 122 | ..\packages\System.Diagnostics.DiagnosticSource.4.3.0\lib\net46\System.Diagnostics.DiagnosticSource.dll 123 | 124 | 125 | ..\packages\System.Globalization.Calendars.4.3.0\lib\net46\System.Globalization.Calendars.dll 126 | 127 | 128 | ..\packages\System.IdentityModel.Tokens.Jwt.4.0.4.403061554\lib\net45\System.IdentityModel.Tokens.Jwt.dll 129 | 130 | 131 | ..\packages\System.IO.Compression.4.3.0\lib\net46\System.IO.Compression.dll 132 | 133 | 134 | 135 | ..\packages\System.IO.Compression.ZipFile.4.3.0\lib\net46\System.IO.Compression.ZipFile.dll 136 | 137 | 138 | ..\packages\System.IO.FileSystem.4.3.0\lib\net46\System.IO.FileSystem.dll 139 | 140 | 141 | ..\packages\System.IO.FileSystem.Primitives.4.3.0\lib\net46\System.IO.FileSystem.Primitives.dll 142 | 143 | 144 | 145 | ..\packages\System.Net.Http.4.3.1\lib\net46\System.Net.Http.dll 146 | 147 | 148 | ..\packages\Microsoft.AspNet.WebApi.Client.5.2.3\lib\net45\System.Net.Http.Formatting.dll 149 | 150 | 151 | 152 | ..\packages\System.Net.Sockets.4.3.0\lib\net46\System.Net.Sockets.dll 153 | 154 | 155 | 156 | ..\packages\System.Runtime.CompilerServices.Unsafe.4.3.0\lib\netstandard1.0\System.Runtime.CompilerServices.Unsafe.dll 157 | 158 | 159 | ..\packages\System.Runtime.InteropServices.RuntimeInformation.4.3.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll 160 | 161 | 162 | 163 | ..\packages\System.Security.Cryptography.Algorithms.4.3.0\lib\net46\System.Security.Cryptography.Algorithms.dll 164 | 165 | 166 | ..\packages\System.Security.Cryptography.Encoding.4.3.0\lib\net46\System.Security.Cryptography.Encoding.dll 167 | 168 | 169 | ..\packages\System.Security.Cryptography.Primitives.4.3.0\lib\net46\System.Security.Cryptography.Primitives.dll 170 | 171 | 172 | ..\packages\System.Security.Cryptography.X509Certificates.4.3.0\lib\net46\System.Security.Cryptography.X509Certificates.dll 173 | 174 | 175 | ..\packages\System.Spatial.5.8.2\lib\net40\System.Spatial.dll 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | ..\packages\Microsoft.AspNet.WebApi.Core.5.2.3\lib\net45\System.Web.Http.dll 188 | 189 | 190 | ..\packages\Microsoft.AspNet.WebApi.WebHost.5.2.3\lib\net45\System.Web.Http.WebHost.dll 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | ..\packages\System.Xml.ReaderWriter.4.3.0\lib\net46\System.Xml.ReaderWriter.dll 199 | 200 | 201 | 202 | 203 | 204 | 205 | Designer 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | Global.asax 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | Web.config 227 | 228 | 229 | Web.config 230 | 231 | 232 | 233 | 234 | 10.0 235 | $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) 236 | 237 | 238 | true 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | True 248 | True 249 | 3979 250 | / 251 | http://localhost:3979/ 252 | False 253 | False 254 | 255 | 256 | False 257 | 258 | 259 | 260 | 261 | 268 | -------------------------------------------------------------------------------- /RemoteControlBotSample/Web.Debug.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 17 | 18 | 29 | 30 | -------------------------------------------------------------------------------- /RemoteControlBotSample/Web.Release.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 17 | 18 | 19 | 30 | 31 | -------------------------------------------------------------------------------- /RemoteControlBotSample/Web.config: -------------------------------------------------------------------------------- 1 |  2 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | -------------------------------------------------------------------------------- /RemoteControlBotSample/default.htm: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 |

RemoteControlBotSample

9 |

Describe your bot here and your terms of use etc.

10 |

Visit Bot Framework to register your bot. When you register it, remember to set your bot's endpoint to

https://your_bots_hostname/api/messages

11 | 12 | 13 | -------------------------------------------------------------------------------- /RemoteControlBotSample/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | --------------------------------------------------------------------------------