Skip to content

Instantly share code, notes, and snippets.

@alindgren
Created March 9, 2017 17:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alindgren/dbd044378602ae40b0fc5159de0da9d8 to your computer and use it in GitHub Desktop.
Save alindgren/dbd044378602ae40b0fc5159de0da9d8 to your computer and use it in GitHub Desktop.
Bot Framework example 4

This was an example demoed at "Building an ecommerce bot using the Microsoft Bot Framework" CodeCamp NYC, Octoboer 2016.

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("Receipt card");
reply.Recipient = activity.From;
reply.Type = "message";
reply.Attachments = new List<Attachment>();
List<CardImage> cardImages = new List<CardImage>();
cardImages.Add(new CardImage(url: "https://s3.amazonaws.com/alindgren/clayton_sunset.jpg"));
List<CardAction> cardButtons = new List<CardAction>();
CardAction plButton = new CardAction()
{
Value = "http://alexbotdemo.azurewebsites.net/",
Type = "openUrl",
Title = "Track your order"
};
cardButtons.Add(plButton);
ReceiptItem lineItem1 = new ReceiptItem()
{
Title = "Trying Stuff Until it Works",
Subtitle = null,
Text = null,
Image = new CardImage(url: "https://s3.amazonaws.com/alindgren/trying_stuff_until_it_works.jpg"),
Price = "20.00",
Quantity = "2",
Tap = null
};
ReceiptItem lineItem2 = new ReceiptItem()
{
Title = "Changing Stuff and Seeing What Happens",
Subtitle = null,
Text = null,
Image = new CardImage(url: "https://s3.amazonaws.com/alindgren/changing_stuff_seing_what_happens.jpg"),
Price = "25.00",
Quantity = "3",
Tap = null
};
List<ReceiptItem> receiptList = new List<ReceiptItem>();
receiptList.Add(lineItem1);
receiptList.Add(lineItem2);
ReceiptCard plCard = new ReceiptCard()
{
Title = "Your receipt",
Buttons = cardButtons,
Items = receiptList,
Total = "101.65",
Tax = "6.65"
};
Attachment plAttachment = plCard.ToAttachment();
reply.Attachments.Add(plAttachment);
await connector.Conversations.SendToConversationAsync(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