Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

helper to send a trace activity #1355

Merged
merged 2 commits into from
Oct 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions libraries/botbuilder-core/src/turnContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,38 @@ export class TurnContext {
return reference;
}

/**
* Asynchronously sends an activity to the sender of the incoming activity.
*
* @param name The activity or text to send.
* @param value Optional. The text to be spoken by your bot on a speech-enabled channel.
* @param valueType Optional. Indicates whether your bot is accepting, expecting, or ignoring user
* @param label Optional. Indicates whether your bot is accepting, expecting, or ignoring user
*
* @remarks
* Creates and sends a Trace activity. Trace activities are only sent when the channel is the emulator.
*
* For example:
* ```JavaScript
* await context.sendTraceActivity(`The following exception was thrown ${msg}`);
* ```
*
* **See also**
*
* - [sendActivities](xref:botbuilder-core.TurnContext.sendActivities)
*/
public sendTraceActivity(name: string, value?: any, valueType?: string, label?: string): Promise<ResourceResponse|undefined> {
const traceActivity: Partial<Activity> = {
type: ActivityTypes.Trace,
timestamp: new Date(),
name: name,
value: value,
valueType: valueType,
label: label
};
return this.sendActivity(traceActivity)
}

/**
* Asynchronously sends an activity to the sender of the incoming activity.
*
Expand Down
20 changes: 18 additions & 2 deletions libraries/botbuilder-core/tests/turnContext.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const assert = require('assert');
const { BotAdapter, MessageFactory, TurnContext } = require('../');
const { BotAdapter, MessageFactory, TurnContext, ActivityTypes } = require('../');

const activityId = `activity ID`;

Expand Down Expand Up @@ -164,7 +164,23 @@ describe(`TurnContext`, function () {
});
context.sendActivity('test', 'say test', 'ignoringInput').then(() => done());
});


it(`should send a trace activity.`, function (done) {
const context = new TurnContext(new SimpleAdapter(), testMessage);
context.onSendActivities((ctx, activities, next) => {
assert(Array.isArray(activities), `activities not array.`);
assert(activities.length === 1, `invalid count of activities.`);
assert(activities[0].type === ActivityTypes.Trace, `type wrong.`);
assert(activities[0].name === 'name-text', `name wrong.`);
assert(activities[0].value === 'value-text', `value worng.`);
assert(activities[0].valueType === 'valueType-text', `valeuType wrong.`);
assert(activities[0].label === 'label-text', `label wrong.`);
return[];
});
context.sendTraceActivity('name-text', 'value-text', 'valueType-text', 'label-text').then(() => done());
});


it(`should send multiple activities via sendActivities().`, function (done) {
const context = new TurnContext(new SimpleAdapter(), testMessage);
context.sendActivities([testMessage, testMessage, testMessage]).then((responses) => {
Expand Down