Created
August 11, 2017 18:00
-
-
Save Krumelur/f86a3163c886e0fe8c2b37a428eadebc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Threading.Tasks; | |
using System.Web.Http; | |
using Microsoft.Bot.Builder.Dialogs; | |
using Microsoft.Bot.Connector; | |
using System.Linq; | |
namespace XamUBot | |
{ | |
/// <summary> | |
/// This controller handles all incoming messages the bot can process. | |
/// </summary> | |
[BotAuthentication] | |
[RoutePrefix("api/messages")] | |
public class MessagesController : ApiController | |
{ | |
/// <summary> | |
/// POST: api/Messages | |
/// Receive a message from a user and reply to it | |
/// </summary> | |
[HttpPost] | |
[Route("")] | |
public async Task<IHttpActionResult> HandleIncomingActivity([FromBody]Activity activity) | |
{ | |
if(activity == null) | |
{ | |
return BadRequest("No activity provided."); | |
} | |
if (activity.Type == ActivityTypes.ConversationUpdate) | |
{ | |
IConversationUpdateActivity update = activity; | |
// Remove bot from the members added | |
update.MembersAdded = update.MembersAdded.Where(member => member.Id != update.Recipient.Id).ToList(); | |
if (update.MembersAdded.Count == 0) | |
{ | |
return Ok(); | |
} | |
} | |
await Conversation.SendAsync(activity, () => new Dialogs.SourceDialog()); | |
return Ok(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Bot.Builder.Dialogs; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using System.Threading.Tasks; | |
using Microsoft.Bot.Connector; | |
namespace XamUBot.Dialogs | |
{ | |
[Serializable] | |
public class SourceDialog : IDialog<object> | |
{ | |
public Task StartAsync(IDialogContext context) | |
{ | |
WaitForNextMessage(context); | |
return Task.CompletedTask; | |
} | |
protected void WaitForNextMessage(IDialogContext context) | |
{ | |
context.Wait(OnInnerMessageReceivedAsync); | |
} | |
protected static PromptOptions<string> CreateDefaultPromptOptions(string title, int numRetries, params string[] buttons) | |
{ | |
return new PromptOptions<string>( | |
prompt: title, | |
retry: title, | |
tooManyAttempts: "", | |
options: new List<string>(buttons), | |
attempts: numRetries, | |
promptStyler: null); | |
} | |
/// <summary> | |
/// Handles messages posted to the dialog. | |
/// </summary> | |
/// <param name="context"></param> | |
/// <param name="result"></param> | |
/// <returns></returns> | |
async Task OnInnerMessageReceivedAsync(IDialogContext context, IAwaitable<object> result) | |
{ | |
var activity = await result as Activity; | |
if (activity.Type == ActivityTypes.Message) | |
{ | |
await context.PostAsync("You said " + activity.Text); | |
PromptDialog.Choice( | |
context, | |
OnChoiceMade, | |
CreateDefaultPromptOptions("Select", 1, "Yes", "No")); | |
} | |
else | |
{ | |
WaitForNextMessage(context); | |
} | |
} | |
async Task OnChoiceMade(IDialogContext context, IAwaitable<object> result) | |
{ | |
var v = await result as string; | |
await context.Forward(new TargetDialog(), OnResumeAfterDialog, null); | |
WaitForNextMessage(context); | |
} | |
async Task OnResumeAfterDialog(IDialogContext context, IAwaitable<object> result) | |
{ | |
await context.PostAsync("Back to source dialog."); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Bot.Builder.Dialogs; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using System.Threading.Tasks; | |
using Microsoft.Bot.Connector; | |
namespace XamUBot.Dialogs | |
{ | |
[Serializable] | |
public class TargetDialog : IDialog<object> | |
{ | |
public async Task StartAsync(IDialogContext context) | |
{ | |
await context.PostAsync("In the target dialog."); | |
WaitForNextMessage(context); | |
} | |
protected void WaitForNextMessage(IDialogContext context) | |
{ | |
context.Wait(OnInnerMessageReceivedAsync); | |
} | |
Task OnInnerMessageReceivedAsync(IDialogContext context, IAwaitable<object> result) | |
{ | |
//Nothing | |
return Task.CompletedTask; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment