Skip to content

Instantly share code, notes, and snippets.

@jamesmundy
Last active November 19, 2018 23:55
Show Gist options
  • Save jamesmundy/f0df379bb322086789f6441e9b914520 to your computer and use it in GitHub Desktop.
Save jamesmundy/f0df379bb322086789f6441e9b914520 to your computer and use it in GitHub Desktop.
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[]
{
activity
},CancellationToken.None))
{
return BotAuthenticator.GenerateUnauthorizedResponse(req);
}
// 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>(),
Conversation.Container.Resolve<ITableService>(),
Conversation.Container.Resolve<IBugTrackingService>(),
Conversation.Container.Resolve<IAnalyticsService>()));
}
return new HttpResponseMessage(HttpStatusCode.OK);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment