Skip to content
This repository has been archived by the owner on Sep 20, 2024. It is now read-only.

Commit

Permalink
Fixes #1920 - Use contextual adapter when spawning bots,
Browse files Browse the repository at this point in the history
  • Loading branch information
benbrown committed Mar 11, 2020
1 parent a1637af commit 6631a61
Show file tree
Hide file tree
Showing 8 changed files with 596 additions and 561 deletions.
2 changes: 1 addition & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* NEW: At long last, the convo.ask callbacks can receive the full incoming message payload in addition to the text content.
This allows developers to use payload values inside quick replies, button clicks and other rich operations. Many thanks to [@naikus](https://github.com/naikus) for the effort and patience it took to get this in! [PR #1801](https://github.com/howdyai/botkit/pull/1801)


* Multi-adapter support improved. Botkit will now spawn the appropriate type of Botworker when used in a multi-adapter scenario. [See this example for a demonstration of using multiple adapters in a single bot app](./packages/testbot/multiadapter.js). [Issue #1920](https://github.com/howdyai/botkit/issues/1920)

# 4.6.1

Expand Down
6 changes: 3 additions & 3 deletions packages/botbuilder-adapter-slack/src/botworker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ export class SlackBotWorker extends BotWorker {
msg.conversation.thread_ts = src.incoming_message.channelData.thread_ts;
}

msg = this.controller.adapter.activityToSlack(msg);
msg = this.getConfig('context').adapter.activityToSlack(msg);

const requestOptions = {
uri: src.incoming_message.channelData.response_url,
Expand Down Expand Up @@ -336,7 +336,7 @@ export class SlackBotWorker extends BotWorker {
* @param update An object in the form `{id: <id of message to update>, conversation: { id: <channel> }, text: <new text>, card: <array of card objects>}`
*/
public async updateMessage(update: Partial<BotkitMessage>): Promise<any> {
return this.controller.adapter.updateActivity(
return this.getConfig('context').adapter.updateActivity(
this.getConfig('context'),
update
);
Expand All @@ -356,7 +356,7 @@ export class SlackBotWorker extends BotWorker {
* @param update An object in the form of `{id: <id of message to delete>, conversation: { id: <channel of message> }}`
*/
public async deleteMessage(update: Partial<BotkitMessage>): Promise<any> {
return this.controller.adapter.deleteActivity(
return this.getConfig('context').adapter.deleteActivity(
this.getConfig('context'),
{
activityId: update.id,
Expand Down
6 changes: 3 additions & 3 deletions packages/botkit/src/botworker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ export class BotWorker {
);

// create a turn context
const turnContext = new TurnContext(this._controller.adapter, activity as Activity);
const turnContext = new TurnContext(this.getConfig('context').adapter, activity as Activity);

// create a new dialogContext so beginDialog works.
const dialogContext = await this._controller.dialogSet.createContext(turnContext);
Expand All @@ -250,7 +250,7 @@ export class BotWorker {

// Create conversation
const parameters: ConversationParameters = { bot: reference.bot, members: [reference.user], isGroup: false, activity: null, channelData: null };
const client = this.controller.adapter.createConnectorClient(reference.serviceUrl);
const client = this.getConfig('context').adapter.createConnectorClient(reference.serviceUrl);

// Mix in the tenant ID if specified. This is required for MS Teams.
if (reference.conversation && reference.conversation.tenantId) {
Expand Down Expand Up @@ -282,7 +282,7 @@ export class BotWorker {
if (response.serviceUrl) { request.serviceUrl = response.serviceUrl; }

// Create context and run middleware
const turnContext: TurnContext = this.controller.adapter.createContext(request);
const turnContext: TurnContext = this.getConfig('context').adapter.createContext(request);

// create a new dialogContext so beginDialog works.
const dialogContext = await this._controller.dialogSet.createContext(turnContext);
Expand Down
10 changes: 6 additions & 4 deletions packages/botkit/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
import { Activity, MemoryStorage, Storage, ConversationReference, TurnContext } from 'botbuilder';
import { Activity, MemoryStorage, Storage, ConversationReference, TurnContext, BotAdapter } from 'botbuilder';
import { Dialog, DialogContext, DialogSet, DialogTurnStatus, WaterfallDialog } from 'botbuilder-dialogs';
import { BotkitBotFrameworkAdapter } from './adapter';
import { BotWorker } from './botworker';
Expand Down Expand Up @@ -1057,8 +1057,9 @@ export class Botkit {
* The spawned `bot` contains all information required to process outbound messages and handle dialog state, and may also contain extensions
* for handling platform-specific events or activities.
* @param config {any} Preferably receives a DialogContext, though can also receive a TurnContext. If excluded, must call `bot.changeContext(reference)` before calling any other method.
* @param adapter {BotAdapter} An optional reference to a specific adapter from which the bot will be spawned. If not specified, will use the adapter from which the configuration object originates. Required for spawning proactive bots in a multi-adapter scenario.
*/
public async spawn(config?: any): Promise<BotWorker> {
public async spawn(config?: any, custom_adapter?: BotAdapter): Promise<BotWorker> {
if (config instanceof TurnContext) {
config = {
dialogContext: await this.dialogSet.createContext(config as TurnContext),
Expand All @@ -1076,8 +1077,9 @@ export class Botkit {
}

let worker: BotWorker = null;
if (this.adapter.botkit_worker) {
const CustomBotWorker = this.adapter.botkit_worker;
const adapter = custom_adapter || config.context.adapter || this.adapter;
if (adapter.botkit_worker) {
const CustomBotWorker = adapter.botkit_worker;
worker = new CustomBotWorker(this, config);
} else {
worker = new BotWorker(this, config);
Expand Down
Loading

0 comments on commit 6631a61

Please sign in to comment.