microsoft / botframework-sdk

Bot Framework provides the most comprehensive experience for building conversation applications.
MIT License
7.48k stars 2.44k forks source link

Provide examples for framework-agnostic integration. #2524

Closed dkushner closed 7 years ago

dkushner commented 7 years ago

I could not find a single example that did not tightly couple itself to Restify as the HTTP framework of choice, however none of the features provided by the framework appear to rely strongly on any Restify features. Would it be possible to get some framework-agnostic examples? Is this even possible with the current codebase?

delight-by commented 7 years ago

You may get an idea on how to decouple BotBuilder from Restify/Express looking at BotServiceConnector class they wrote to support Azure Functions: BotServiceConnector.ts. I've done similar thing for Koa w/Promises and never looked back.

nwhitmont commented 7 years ago

@dkushner Restify is a Node.js server module designed specifically for API applications. You can replace Restify with any server framework of your choice, for example Express or Hapi, but it may require additional configuration not described in the examples. We recommend Restify since it is compact and easy to configure, but there is certainly no reason not to use another server module if you are more comfortable with it.

sethreidnz commented 6 years ago

@delight-by Care to share your solution for koa?

delight-by commented 6 years ago

@sethreidnz Sure, check this out: https://github.com/delight-by/botbuilder-koa.

sethreidnz commented 6 years ago

Nice! And damn I already converted to restify. But I might go back because I love async await too much to live without it now. Let me know if you want any help, might write a blog post about how to do Bot Builder with Koa.

mattcasey commented 3 years ago

In case anyone finds this thread, because I spent an entire 24 hours figuring out how to make v4 of botframework work with koa, here's my working code. The trick is just to make sure you satisfy the WebRequest and WebResponse interfaces:

import { BotFrameworkAdapter, WebRequest, WebResponse } from 'botbuilder';
import { TeamsMessagingExtensionsActionBot } from './bots/teamsMessagingExtensionsActionBot';
import Router from '@koa/router';

// Create adapter.
// See https://aka.ms/about-bot-adapter to learn more about adapters.
const adapter = new BotFrameworkAdapter({
  appId: process.env.MICROSOFT_APP_ID,
  appPassword: process.env.MICROSOFT_APP_PASSWORD
});

// Create the bot that will handle incoming messages.
const bot = new TeamsMessagingExtensionsActionBot();

router.post('/api/messages',  async function (ctx, next) {

  await new Promise((resolve) => {

    const req: WebRequest = {
      body: ctx.request.body,
      headers: ctx.request.headers,
      method: ctx.request.method,
      params: ctx.params,
      query: ctx.query
    };

    const res: WebResponse = {
      send (body?: any) {
        ctx.body = body;
      },
      status (code: number) {
        ctx.status = code;
      },
      end () {
        resolve(null);
      }
    };

    adapter.processActivity(req, res, async (context) => {
      await bot.run(context);
    });
  });
});