-
Notifications
You must be signed in to change notification settings - Fork 128
Add locale support in DirectLine options #293
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
Merged
compulim
merged 8 commits into
master
from
zhiwang/add_locale_and_userid_username_options
Aug 5, 2020
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5598595
add locale userid and username support
timenick cb44dca
change to locale support only
timenick bcc9074
remove unused parameters
timenick 1c78ba8
change locale to conversationStartLocale
timenick 3412062
change to conversation start properties JSON
timenick abd4135
Merge branch 'master' into zhiwang/add_locale_and_userid_username_opt…
timenick 2168453
change the test bot endpoint and add locale tests
timenick 8fd7dc9
fixed comments
timenick File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| import 'dotenv/config'; | ||
|
|
||
| import onErrorResumeNext from 'on-error-resume-next'; | ||
|
|
||
| import { timeouts } from './constants.json'; | ||
| import * as createDirectLine from './setup/createDirectLine'; | ||
| import waitForBotToRespond from './setup/waitForBotToRespond'; | ||
|
|
||
| describe('Happy path', () => { | ||
| let unsubscribes; | ||
|
|
||
| beforeEach(() => unsubscribes = []); | ||
| afterEach(() => unsubscribes.forEach(fn => onErrorResumeNext(fn))); | ||
|
|
||
| describe('should receive the welcome message from bot in English', () => { | ||
| let directLine; | ||
|
|
||
| describe('using REST', () => { | ||
| beforeEach(() => jest.setTimeout(timeouts.rest)); | ||
|
|
||
| test('without conversation start properties', async () => { | ||
| directLine = await createDirectLine.forREST({ token: true }); | ||
| }); | ||
|
|
||
| test('without locale in conversation start properties', async () => { | ||
| directLine = await createDirectLine.forREST({ token: true }, { conversationStartProperties: {} }); | ||
| }); | ||
|
|
||
| test('with locale "en-US" in conversation start properties', async () => { | ||
| directLine = await createDirectLine.forREST({ token: true }, { conversationStartProperties: { locale: 'en-US' } }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('using Web Socket', () => { | ||
| beforeEach(() => jest.setTimeout(timeouts.webSocket)); | ||
|
|
||
| test('without conversation start properties', async () => { | ||
| directLine = await createDirectLine.forWebSocket({ token: true }); | ||
| }); | ||
|
|
||
| test('without locale in conversation start properties', async () => { | ||
| directLine = await createDirectLine.forWebSocket({ token: true }, { conversationStartProperties: {} }); | ||
| }); | ||
|
|
||
| test('with locale "en-US" in conversation start properties', async () => { | ||
| directLine = await createDirectLine.forWebSocket({ token: true }, { conversationStartProperties: { locale: 'en-US' } }); | ||
| }); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| // If directLine object is undefined, that means the test is failing. | ||
| if (!directLine) { return; } | ||
|
|
||
| unsubscribes.push(directLine.end.bind(directLine)); | ||
|
|
||
| await waitForBotToRespond(directLine, ({ text }) => text === 'Welcome'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('should receive the welcome message from bot in Chinese', () => { | ||
| let directLine; | ||
|
|
||
| describe('using REST', () => { | ||
| beforeEach(() => jest.setTimeout(timeouts.rest)); | ||
|
|
||
| test('with locale "zh-CN" in conversation start properties', async () => { | ||
| directLine = await createDirectLine.forREST({ token: true }, { conversationStartProperties: { locale: 'zh-CN' } }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('using Web Socket', () => { | ||
| beforeEach(() => jest.setTimeout(timeouts.webSocket)); | ||
|
|
||
| test('with locale "zh-CN" in conversation start properties', async () => { | ||
| directLine = await createDirectLine.forWebSocket({ token: true }, { conversationStartProperties: { locale: 'zh-CN' } }); | ||
| }); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| // If directLine object is undefined, that means the test is failing. | ||
| if (!directLine) { return; } | ||
|
|
||
| unsubscribes.push(directLine.end.bind(directLine)); | ||
|
|
||
| await waitForBotToRespond(directLine, ({ text }) => text === '欢迎'); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import 'dotenv/config'; | ||
|
|
||
| import onErrorResumeNext from 'on-error-resume-next'; | ||
|
|
||
| import { timeouts } from './constants.json'; | ||
| import * as createDirectLine from './setup/createDirectLine'; | ||
| import waitForBotToRespond from './setup/waitForBotToRespond'; | ||
|
|
||
| describe('Unhappy path', () => { | ||
| let unsubscribes; | ||
|
|
||
| beforeEach(() => unsubscribes = []); | ||
| afterEach(() => unsubscribes.forEach(fn => onErrorResumeNext(fn))); | ||
|
|
||
| describe('should receive the welcome message from bot in English', () => { | ||
| let directLine; | ||
|
|
||
| describe('using REST', () => { | ||
| beforeEach(() => jest.setTimeout(timeouts.rest)); | ||
|
|
||
| test('with invalid locale in conversation start properties', async () => { | ||
| directLine = await createDirectLine.forREST({ token: true }, { conversationStartProperties: { locale: { x: 'test' } } }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('using Web Socket', () => { | ||
| beforeEach(() => jest.setTimeout(timeouts.webSocket)); | ||
|
|
||
| test('with invalid locale in conversation start properties', async () => { | ||
| directLine = await createDirectLine.forWebSocket({ token: true }, { conversationStartProperties: { locale: { x: 'test' } } }); | ||
| }); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| // If directLine object is undefined, that means the test is failing. | ||
| if (!directLine) { return; } | ||
|
|
||
| unsubscribes.push(directLine.end.bind(directLine)); | ||
|
|
||
| await waitForBotToRespond(directLine, ({ text }) => text === 'Welcome'); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -370,7 +370,8 @@ export interface DirectLineOptions { | |
| streamUrl?: string, | ||
| timeout?: number, | ||
| // Attached to all requests to identify requesting agent. | ||
| botAgent?: string | ||
| botAgent?: string, | ||
| conversationStartProperties?: any | ||
| } | ||
|
|
||
| export interface Services { | ||
|
|
@@ -469,6 +470,8 @@ export class DirectLine implements IBotConnection { | |
| private timeout = 20 * 1000; | ||
| private retries: number; | ||
|
|
||
| private localeOnStartConversation: string; | ||
|
|
||
| private pollingInterval: number = 1000; //ms | ||
|
|
||
| private tokenRefreshSubscription: Subscription; | ||
|
|
@@ -478,6 +481,14 @@ export class DirectLine implements IBotConnection { | |
| this.token = options.secret || options.token; | ||
| this.webSocket = (options.webSocket === undefined ? true : options.webSocket) && typeof WebSocket !== 'undefined' && WebSocket !== undefined; | ||
|
|
||
| if (options.conversationStartProperties && options.conversationStartProperties.locale) { | ||
| if (Object.prototype.toString.call(options.conversationStartProperties.locale) === '[object String]') { | ||
| this.localeOnStartConversation = options.conversationStartProperties.locale; | ||
| } else { | ||
| console.warn('DirectLineJS: conversationStartProperties.locale was ignored: the locale name may be a BCP 47 language tag'); | ||
| } | ||
| } | ||
|
|
||
| if (options.domain) { | ||
| this.domain = options.domain; | ||
| } | ||
|
|
@@ -616,12 +627,19 @@ export class DirectLine implements IBotConnection { | |
| ? `${this.domain}/conversations/${this.conversationId}?watermark=${this.watermark}` | ||
| : `${this.domain}/conversations`; | ||
| const method = this.conversationId ? "GET" : "POST"; | ||
| const body = this.conversationId | ||
| ? undefined | ||
| : { | ||
| locale: this.localeOnStartConversation | ||
| }; | ||
| return this.services.ajax({ | ||
| method, | ||
| url, | ||
| body, | ||
| timeout: this.timeout, | ||
| headers: { | ||
| "Accept": "application/json", | ||
| "Content-Type": "application/json", | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a test to make sure adding a Content-Type doesnt affect existing clients that dont send a body. |
||
| ...this.commonHeaders() | ||
| } | ||
| }) | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if conversationStartProperties is not sent in or does not contain a locale, should we still send a body? I would assume not since we dont want to change any existing client.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In our backend, we are reading from body to get the conversationStartParameters. Because we are not only sending locale in body, we would send more parameters (like userid, username, etc) in the future. The locale parameter is optional, it would not affect any existing client.