Skip to content

Instantly share code, notes, and snippets.

View jamesmundy's full-sized avatar

James Mundy jamesmundy

View GitHub Profile
@jamesmundy
jamesmundy / CompleteAzureFunctionBotFrameworkBot.cs
Last active November 19, 2018 23:55
The full code needed to create a bot framework bot and host on Azure Functions
[FunctionName("ChatFunction")]
public static async Task<HttpResponseMessage> ChatFunction([HttpTrigger(AuthorizationLevel.Anonymous, Route = "v1/messages")] HttpRequestMessage req,
TraceWriter log)
{
using (BotService.Initialize())
{
// authenticate incoming request and add activity.ServiceUrl to MicrosoftAppCredentials.TrustedHostNames
// if request is authenticated
if (!await BotService.Authenticator.TryAuthenticateAsync(req, new[]
{
@jamesmundy
jamesmundy / BotFrameworkDeserializeRequest.cs
Last active November 19, 2018 23:56
Deserializes requests and returns dialogs
// Deserialize the incoming activity
string jsonContent = await req.Content.ReadAsStringAsync();
var activity = JsonConvert.DeserializeObject<Activity>(jsonContent);
if (activity != null)
{
if (activity.Type == ActivityTypes.Message)
await Conversation.SendAsync(activity,() =>
new NewPostcardDialog(Conversation.Container.Resolve<IAppSettingsService>(),
Conversation.Container.Resolve<IPaypalService>(),
@jamesmundy
jamesmundy / RegisterBotFrameworkDependencies.cs
Last active November 19, 2018 23:57
Register Bot Framework Dependencies
private static void RegisterDependencies(IAppSettingsService settingsService)
{
var store = new TableBotDataStore(settingsService.StorageConnectionString);
Conversation.UpdateContainer(builder =>
{
builder.RegisterModule(new DefaultExceptionMessageOverrideModule());
builder.Register(c => store)
.Keyed<IBotDataStore<BotData>>(AzureModule.Key_DataStore)
.AsSelf()
.SingleInstance();
@jamesmundy
jamesmundy / AzureFunctionBotFrameworkAuthorization.cs
Last active November 19, 2018 23:57
Code showing how to set up your Azure Function to authorize bot framework requests
using (BotService.Initialize())
{
// authenticate incoming request and add activity.ServiceUrl to MicrosoftAppCredentials.TrustedHostNames
// if request is authenticated
if (!await BotService.Authenticator.TryAuthenticateAsync(req, new[]
{
activity
},CancellationToken.None))
{
return BotAuthenticator.GenerateUnauthorizedResponse(req);
@jamesmundy
jamesmundy / AzureFunctionBotFrameworkBotInit.cs
Last active November 19, 2018 23:58
The initial set up of an Azure Function http trigger for an Azure Function bot
[FunctionName("ChatFunction")]
public static async Task<HttpResponseMessage> ChatFunction([HttpTrigger(AuthorizationLevel.Anonymous, Route = "v1/messages")] HttpRequestMessage req,
TraceWriter log)
{
return new HttpResponseMessage(HttpStatusCode.OK);
}
@jamesmundy
jamesmundy / WebApiConfigFileBotFramework.cs
Last active November 19, 2018 23:53
An example WebAPIConfig file used to configure a bot framework web api.
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
var store = new TableBotDataStore(ConfigurationManager.ConnectionStrings["StorageConnectionString"]
.ConnectionString);
Conversation.UpdateContainer(builder => {
builder.RegisterModule(new DefaultExceptionMessageOverrideModule());
builder.Register(c => store)
@jamesmundy
jamesmundy / BotFrameworkEndpoint.cs
Last active November 19, 2018 23:58
The Bot entry point for an ASP.NET Web Api
[BotAuthentication]
public class MessagesController : ApiController
{
private readonly ITableService _tableService;
public MessagesController(ITableService tableService)
{
_tableService = tableService;
}
public class TestSample
{
public int HighestProductOfThree(int[] arrayOfInts)
{
if (arrayOfInts.Length < 3)
throw new ArgumentException("Less than 3 items!");
// We're going to start at the 3rd item (at index 2)
// so pre-populate highests and lowests based on the first 2 items.
// we could also start these as null and check below if they're set
@jamesmundy
jamesmundy / MKMapViewZoomLevel.cs
Last active January 21, 2016 14:17 — forked from djad442/MKMapViewZoomLevel.cs
MonoTouch MKMapView with Zoom Level Property
using System;
using MapKit;
using CoreLocation;
using CoreGraphics;
using System.Drawing;
using Foundation;
namespace BinaryQuest
{
[Register("MKMapViewZoomLevel")]
<ScrollViewer x:Name="ScrollViewer" AutomationProperties.AccessibilityView="Raw" BringIntoViewOnFocusChange="{TemplateBinding ScrollViewer.BringIntoViewOnFocusChange}" HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}" IsHorizontalRailEnabled="{TemplateBinding ScrollViewer.IsHorizontalRailEnabled}" IsVerticalRailEnabled="{TemplateBinding ScrollViewer.IsVerticalRailEnabled}" IsDeferredScrollingEnabled="{TemplateBinding ScrollViewer.IsDeferredScrollingEnabled}" Padding="{TemplateBinding Padding}" TabNavigation="{TemplateBinding TabNavigation}" VerticalScrollBarVisibility="{TemplateBinding ScrollViewer.VerticalScrollBarVisibility}" ZoomMode="{TemplateBinding ScrollViewer.ZoomMode}" VerticalScrollMode="Disabled">
<ItemsPresenter/>
</ScrollViewer>