This was an example demoed at "Building an ecommerce bot using the Microsoft Bot Framework" CodeCamp NYC, Octoboer 2016.
Created
March 9, 2017 17:57
-
-
Save alindgren/dab5c14ae1e0917b40eab09c65fe5717 to your computer and use it in GitHub Desktop.
Bot Framework example 3
This file contains hidden or 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; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Net; | |
using System.Net.Http; | |
using System.Web.Http; | |
using Microsoft.Bot.Connector; | |
using System.Threading.Tasks; | |
namespace BotDemo.Controllers | |
{ | |
public class BotController : ApiController | |
{ | |
/// <summary> | |
/// POST: api/Bot | |
/// Receive a message from a user and reply to it | |
/// </summary> | |
public async Task<HttpResponseMessage> Post([FromBody]Activity activity) | |
{ | |
if (activity.Type == ActivityTypes.Message) | |
{ | |
ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl)); | |
// calculate something for us to return | |
int length = (activity.Text ?? string.Empty).Length; | |
StateClient stateClient = activity.GetStateClient(); | |
BotData userData = await stateClient.BotState.GetUserDataAsync(activity.ChannelId, activity.From.Id); | |
if (activity.Text.Contains("hi") || activity.Text.Contains("hello")) | |
{ | |
Activity reply = activity.CreateReply(); | |
reply.Attachments.Add(new Attachment() | |
{ | |
ContentUrl = "https://s3.amazonaws.com/alindgren/clayton_sunset.jpg", | |
ContentType = "image/jpg", | |
Name = "Clayton, NY sunset" | |
}); | |
await connector.Conversations.ReplyToActivityAsync(reply); | |
} | |
else | |
{ | |
// return our reply to the user | |
Activity reply = activity.CreateReply($"You sent {activity.Text} which was {length} characters"); | |
await connector.Conversations.ReplyToActivityAsync(reply); | |
} | |
} | |
else | |
{ | |
HandleSystemMessage(activity); | |
} | |
var response = Request.CreateResponse(HttpStatusCode.OK); | |
return response; | |
} | |
private Activity HandleSystemMessage(Activity message) | |
{ | |
if (message.Type == ActivityTypes.DeleteUserData) | |
{ | |
// Implement user deletion here | |
// If we handle user deletion, return a real message | |
} | |
else if (message.Type == ActivityTypes.ConversationUpdate) | |
{ | |
// Handle conversation state changes, like members being added and removed | |
// Use Activity.MembersAdded and Activity.MembersRemoved and Activity.Action for info | |
// Not available in all channels | |
} | |
else if (message.Type == ActivityTypes.ContactRelationUpdate) | |
{ | |
// Handle add/remove from contact lists | |
// Activity.From + Activity.Action represent what happened | |
} | |
else if (message.Type == ActivityTypes.Typing) | |
{ | |
// Handle knowing tha the user is typing | |
} | |
else if (message.Type == ActivityTypes.Ping) | |
{ | |
} | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment