How to use the botbuilder.TurnContext function in botbuilder

To help you get started, we’ve selected a few botbuilder examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github howdyai / botkit / packages / botbuilder-adapter-web / src / web_adapter.ts View on Github external
id: message.user
                        },
                        recipient: {
                            id: 'bot'
                        },
                        channelData: message,
                        text: message.text,
                        type: message.type === 'message' ? ActivityTypes.Message : ActivityTypes.Event
                    };

                    // set botkit's event type
                    if (activity.type !== ActivityTypes.Message) {
                        activity.channelData.botkitEventType = message.type;
                    }

                    const context = new TurnContext(this, activity as Activity);
                    this.runMiddleware(context, logic)
                        .catch((err) => { console.error(err.toString()); });
                } catch (e) {
                    var alert = [
                        `Error parsing incoming message from websocket.`,
                        `Message must be JSON, and should be in the format documented here:`,
                        `https://botkit.ai/docs/readme-web.html#message-objects`
                    ];
                    console.error(alert.join('\n'));
                    console.error(e);
                }
            });
github howdyai / botkit / packages / botbuilder-adapter-slack / src / slack_adapter.ts View on Github external
};

                activity.recipient.id = await this.getBotUserByTeam(activity as Activity);

                // Normalize the location of the team id
                activity.channelData.team = event.team_id;

                // add the team id to the conversation record
                // @ts-ignore -- Tell Typescript to ignore this overload
                activity.conversation.team = activity.channelData.team;

                activity.channelData.botkitEventType = 'slash_command';

                // create a conversation reference
                // @ts-ignore
                const context = new TurnContext(this, activity as Activity);

                context.turnState.set('httpStatus', 200);

                await this.runMiddleware(context, logic);

                // send http response back
                res.status(context.turnState.get('httpStatus'));
                if (context.turnState.get('httpBody')) {
                    res.send(context.turnState.get('httpBody'));
                } else {
                    res.end();
                }
            }
        } else {
            console.error('Unknown Slack event type: ', event);
        }
github huan / botbuilder-wechaty-adapter / src / wechaty-adapter.ts View on Github external
public async continueConversation (
    ref: ConversationReference,
    callback: (context: TurnContext) => Promise,
  ): Promise {
    const activity: Partial = TurnContext.applyConversationReference({}, ref, true)

    try {
      await this.runMiddleware(
        new TurnContext(this, activity),
        callback,
      )
    } catch (err) {
      // TODO: add some code to deal with err...
      throw err
    }
  }
github howdyai / botkit / packages / botbuilder-adapter-hangouts / src / hangouts_adapter.ts View on Github external
activity.type = ActivityTypes.Event;
                activity.channelData.botkitEventType = event.space.type === 'ROOM' ? 'bot_room_join' : 'bot_dm_join';
            }

            if (event.type === 'REMOVED_FROM_SPACE') {
                activity.type = ActivityTypes.Event;
                activity.channelData.botkitEventType = event.space.type === 'ROOM' ? 'bot_room_leave' : 'bot_dm_leave';
            }

            if (event.type === 'CARD_CLICKED') {
                activity.type = ActivityTypes.Event;
                activity.channelData.botkitEventType = event.type.toLowerCase();
            }

            // create a conversation reference
            const context = new TurnContext(this, activity);

            if (event.type !== 'CARD_CLICKED') {
                // send 200 status immediately, otherwise
                // hangouts does not mark the incoming message as received
                res.status(200);
                res.end();
            } else {
                context.turnState.set('httpStatus', 200);
            }

            await this.runMiddleware(context, logic);

            if (event.type === 'CARD_CLICKED') {
                // send http response back
                res.status(context.turnState.get('httpStatus'));
                if (context.turnState.get('httpBody')) {
github huan / botbuilder-wechaty-adapter / src / wechaty-adapter.ts View on Github external
this.wechaty.on('message', async msg => {
      log.verbose('WechatyAdapter', 'listen() wechaty.on(message) %s', msg)

      const activity = await this.buildActivity(msg)
      if (!activity) {
        // should skip this message
        log.verbose('WechatyAdapter', 'listen() wechaty.on(message) no activity')
        return
      }

      try {
        await this.runMiddleware(
          new TurnContext(this, activity),
          logic,
        )
      } catch (err) {
        throw err
      }
    })
github microsoft / botframework-solutions / lib / typescript / botbuilder-skills / src / http / skillHttpBotAdapter.ts View on Github external
public async processActivity(activity: Activity, callback: BotCallbackHandler): Promise {
        const messageIn: string = `SkillHttpBotAdapter: Received an incoming activity. Activity id: ${activity.id}`;
        this.telemetryClient.trackTrace({
            message: messageIn,
            severityLevel: Severity.Information
        });

        // Process the Activity through the Middleware and the Bot, this will generate Activities which we need to send back.
        const context: TurnContext = new TurnContext(this, activity);

        await this.runMiddleware(context, callback);

        const messageOut: string = `SkillHttpBotAdapter: Batching activities in the response. ReplyToId: ${activity.id}`;
        this.telemetryClient.trackTrace({
            message: messageOut,
            severityLevel: Severity.Information
        });

        // Any Activity responses are now available (via SendActivitiesAsync) so we need to pass back for the response
        return {
            status: 200,
            body: this.queuedActivities.splice(0, this.queuedActivities.length)
        };
    }
github howdyai / botkit / packages / botbuilder-adapter-webex / src / webex_adapter.ts View on Github external
public async continueConversation(reference: Partial, logic: (context: TurnContext) => Promise): Promise {
        const request = TurnContext.applyConversationReference(
            { type: 'event', name: 'continueConversation' },
            reference,
            true
        );
        const context = new TurnContext(this, request);

        return this.runMiddleware(context, logic);
    }
github microsoft / botframework-solutions / sdk / typescript / libraries / botbuilder-solutions / src / skills / inProcAdapter.ts View on Github external
public async continueConversation(
        reference: Partial,
        logic: (revocableContext: TurnContext) => Promise): Promise {
        if (reference === undefined) {
            throw new Error('Missing parameter.  reference is required');
        }

        if (logic === undefined) {
            throw new Error('Missing parameter.  logic is required');
        }

        const context: TurnContext = new TurnContext(this, ActivityExtensions.getContinuationActivity(reference));
        await this.runMiddleware(context, logic);
    }
github howdyai / botkit / packages / botbuilder-adapter-hangouts / src / hangouts_adapter.ts View on Github external
public async continueConversation(reference: Partial, logic: (context: TurnContext) => Promise): Promise {
        const request = TurnContext.applyConversationReference(
            { type: 'event', name: 'continueConversation' },
            reference,
            true
        );
        const context = new TurnContext(this, request);

        return this.runMiddleware(context, logic);
    }
github howdyai / botkit / packages / botbuilder-webex / lib / webex_adapter.js View on Github external
return __awaiter(this, void 0, void 0, function* () {
            const request = botbuilder_1.TurnContext.applyConversationReference({ type: 'event', name: 'continueConversation' }, reference, true);
            const context = new botbuilder_1.TurnContext(this, request);
            return this.runMiddleware(context, logic);
        });
    }