Skip to content
This repository has been archived by the owner on Jun 30, 2022. It is now read-only.

[TypeScript][Virtual Assistant & Skill] Fix duplicated messages when communicating through DirectLine #3732

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
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
import {
Activity,
ActivityTypes,
BotState,
BotState,
ChannelAccount,
Channels,
ConversationState,
StatePropertyAccessor,
Expand Down Expand Up @@ -59,18 +60,26 @@ export class DefaultActivityHandler<T extends Dialog> extends TeamsActivityHandl
await this.userState.saveChanges(turnContext, false);
}

protected async membersAdded(turnContext: TurnContext): Promise<void> {
const userProfile: IUserProfileState = await this.userProfileState.get(turnContext, () => { name: ''; });
protected async membersAdded(turnContext: TurnContext, next: () => Promise<void>): Promise<void> {
const membersAdded: ChannelAccount[] = turnContext.activity.membersAdded;
for (const member of membersAdded) {
if (member.id !== turnContext.activity.recipient.id) {
const userProfile: IUserProfileState = await this.userProfileState.get(turnContext, () => {
name: '';
});
if (userProfile.name === undefined || userProfile.name.trim().length === 0) {
// Send new user intro card.
await turnContext.sendActivity(this.templateManager.generateActivityForLocale('NewUserIntroCard', userProfile));
} else {
// Send returning user intro card.
await turnContext.sendActivity(this.templateManager.generateActivityForLocale('ReturningUserIntroCard', userProfile));
}

if (userProfile.name === undefined || userProfile.name.trim().length === 0) {
// Send new user intro card.
await turnContext.sendActivity(this.templateManager.generateActivityForLocale('NewUserIntroCard', userProfile));
} else {
// Send returning user intro card.
await turnContext.sendActivity(this.templateManager.generateActivityForLocale('ReturningUserIntroCard', userProfile));
await DialogEx.run(this.dialog, turnContext, this.dialogStateAccessor);
}
}

await DialogEx.run(this.dialog, turnContext, this.dialogStateAccessor);
// By calling next() you ensure that the next BotHandler is run.
await next();
}

protected async onMessageActivity(turnContext: TurnContext): Promise<void> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,16 @@ describe("Localization", function() {
{
id: "1",
name: "user"
},
{
id: "2",
name: "bot"
}
],
channelId: "emulator",
recipient: {
id: "1"
},
locale: "es-es"
})
.assertReply(function (activity, description) {
Expand Down Expand Up @@ -57,8 +65,16 @@ describe("Localization", function() {
{
id: "1",
name: "user"
},
{
id: "2",
name: "bot"
}
],
channelId: "emulator",
recipient: {
id: "1"
},
locale: "de-de"
})
.assertReply(function (activity, description) {
Expand Down Expand Up @@ -94,8 +110,16 @@ describe("Localization", function() {
{
id: "1",
name: "user"
},
{
id: "2",
name: "bot"
}
],
channelId: "emulator",
recipient: {
id: "1"
},
locale: "fr-fr"
})
.assertReply(function (activity, description) {
Expand Down Expand Up @@ -131,8 +155,16 @@ describe("Localization", function() {
{
id: "1",
name: "user"
},
{
id: "2",
name: "bot"
}
],
channelId: "emulator",
recipient: {
id: "1"
},
locale: "it-it"
})
.assertReply(function (activity, description) {
Expand Down Expand Up @@ -168,8 +200,16 @@ describe("Localization", function() {
{
id: "1",
name: "user"
},
{
id: "2",
name: "bot"
}
],
channelId: "emulator",
recipient: {
id: "1"
},
locale: "en-us"
})
.assertReply(function (activity, description) {
Expand Down Expand Up @@ -205,8 +245,16 @@ describe("Localization", function() {
{
id: "1",
name: "user"
},
{
id: "2",
name: "bot"
}
],
channelId: "emulator",
recipient: {
id: "1"
},
locale: "zh-cn"
})
.assertReply(function (activity, description) {
Expand Down Expand Up @@ -241,15 +289,23 @@ describe("Localization", function() {
{
id: "1",
name: "user"
},
{
id: "2",
name: "bot"
}
],
channelId: "emulator",
recipient: {
id: "1"
},
locale: "en-uk"
})
.assertReply(function (activity, description) {
assert.strictEqual(1, activity.attachments.length);
});

return testNock.resolveWithMocks('localization_response_en-uk', done, flow);
return testNock.resolveWithMocks('localization_response_en-uk', done, flow);
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,17 @@ describe("Main Dialog", function () {
{
id: "1",
name: "user"
},
{
id: "2",
name: "bot"
}
],
channelId: "emulator",
recipient: {
id: "1"
},
locale: "en-us"
})
.assertReply(function (activity, description) {
assert.strictEqual(1, activity.attachments.length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
ActivityHandler,
ActivityTypes,
BotState,
ChannelAccount,
Channels,
StatePropertyAccessor,
TurnContext } from 'botbuilder';
Expand Down Expand Up @@ -42,8 +43,15 @@ export class DefaultActivityHandler<T extends Dialog> extends ActivityHandler {
}

protected async membersAdded(turnContext: TurnContext, next: () => Promise<void>): Promise<void> {
await turnContext.sendActivity(this.templateManager.generateActivityForLocale('IntroMessage'));
await DialogEx.run(this.dialog, turnContext, this.dialogStateAccessor);
const membersAdded: ChannelAccount[] = turnContext.activity.membersAdded;
for (const member of membersAdded) {
if (member.id !== turnContext.activity.recipient.id) {
await turnContext.sendActivity(this.templateManager.generateActivityForLocale('IntroMessage'));
await DialogEx.run(this.dialog, turnContext, this.dialogStateAccessor);
}
}
// By calling next() you ensure that the next BotHandler is run.
await next();
}

protected onMessageActivity(turnContext: TurnContext): Promise<void> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ describe("localization", function() {
{
id: "1",
name: "user"
},
{
id: "2",
name: "bot"
}
],
channelId: "emulator",
Expand All @@ -45,6 +49,10 @@ describe("localization", function() {
{
id: "1",
name: "user"
},
{
id: "2",
name: "bot"
}
],
channelId: "emulator",
Expand All @@ -69,6 +77,10 @@ describe("localization", function() {
{
id: "1",
name: "user"
},
{
id: "2",
name: "bot"
}
],
channelId: "emulator",
Expand All @@ -93,6 +105,10 @@ describe("localization", function() {
{
id: "1",
name: "user"
},
{
id: "2",
name: "bot"
}
],
channelId: "emulator",
Expand All @@ -117,6 +133,10 @@ describe("localization", function() {
{
id: "1",
name: "user"
},
{
id: "2",
name: "bot"
}
],
channelId: "emulator",
Expand All @@ -141,6 +161,10 @@ describe("localization", function() {
{
id: "1",
name: "user"
},
{
id: "2",
name: "bot"
}
],
channelId: "emulator",
Expand All @@ -165,6 +189,10 @@ describe("localization", function() {
{
id: "1",
name: "user"
},
{
id: "2",
name: "bot"
}
],
channelId: "emulator",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ describe("main dialog", function() {
{
id: "1",
name: "user"
},
{
id: "2",
name: "bot"
}
],
channelId: "emulator",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
import {
Activity,
ActivityTypes,
BotState,
BotState,
ChannelAccount,
Channels,
ConversationState,
StatePropertyAccessor,
Expand Down Expand Up @@ -59,18 +60,26 @@ export class DefaultActivityHandler<T extends Dialog> extends TeamsActivityHandl
await this.userState.saveChanges(turnContext, false);
}

protected async membersAdded(turnContext: TurnContext): Promise<void> {
const userProfile: IUserProfileState = await this.userProfileState.get(turnContext, () => { name: ''; });
protected async membersAdded(turnContext: TurnContext, next: () => Promise<void>): Promise<void> {
const membersAdded: ChannelAccount[] = turnContext.activity.membersAdded;
for (const member of membersAdded) {
if (member.id !== turnContext.activity.recipient.id) {
const userProfile: IUserProfileState = await this.userProfileState.get(turnContext, () => {
name: '';
});
if (userProfile.name === undefined || userProfile.name.trim().length === 0) {
// Send new user intro card.
await turnContext.sendActivity(this.templateManager.generateActivityForLocale('NewUserIntroCard', userProfile));
} else {
// Send returning user intro card.
await turnContext.sendActivity(this.templateManager.generateActivityForLocale('ReturningUserIntroCard', userProfile));
}

if (userProfile.name === undefined || userProfile.name.trim().length === 0) {
// Send new user intro card.
await turnContext.sendActivity(this.templateManager.generateActivityForLocale('NewUserIntroCard', userProfile));
} else {
// Send returning user intro card.
await turnContext.sendActivity(this.templateManager.generateActivityForLocale('ReturningUserIntroCard', userProfile));
await DialogEx.run(this.dialog, turnContext, this.dialogStateAccessor);
}
}

await DialogEx.run(this.dialog, turnContext, this.dialogStateAccessor);
// By calling next() you ensure that the next BotHandler is run.
await next();
}

protected async onMessageActivity(turnContext: TurnContext): Promise<void> {
Expand Down
Loading