-
Notifications
You must be signed in to change notification settings - Fork 10
CHI-3641: Add channel_type attribute to new aselo webchat conversations. #4087
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
Open
stephenhand
wants to merge
18
commits into
master
Choose a base branch
from
CHI-3641-channel_type_convo_attribute
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
b7d8fe6
Add channel_type attribute to new aselo webchat conversations. Remove…
stephenhand 4cd237b
Fix reference
stephenhand bf199ae
Remove conversations proxy session test
stephenhand 7175060
Initial plan
Copilot a61a7fd
Add unit tests for initWebchat.ts covering webChannels.create and pat…
Copilot caba9e6
Rework to create conversations like custom channels
stephenhand 59820c6
Licence header
stephenhand fae9c3e
Fix webchat attributes
stephenhand 9f450ed
Fix webchat attributes & getProfileIdentifier for conversations 'web'…
stephenhand ad2937c
Fix tests
stephenhand 1296eb2
Fix Prettier lint errors in initWebchat.test.ts
Copilot 60536ef
Merge pull request #4088 from techmatters/copilot/sub-pr-4087
stephenhand 25bfa84
Rewrite initWebchat.test.ts to align with updated createConversation …
Copilot 8380caf
Merge branch 'master' into CHI-3641-channel_type_convo_attribute
stephenhand 3782d5b
Update lambdas/account-scoped/src/conversation/patchConversationAttri…
stephenhand 6be0704
Update lambdas/account-scoped/src/conversation/patchConversationAttri…
stephenhand 3845943
Fix contactWebchatOrchestrator error handling
stephenhand 79b08ff
Merge remote-tracking branch 'origin/CHI-3641-channel_type_convo_attr…
stephenhand 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
107 changes: 107 additions & 0 deletions
107
lambdas/account-scoped/src/conversation/createConversation.ts
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,107 @@ | ||
| /** | ||
| * Copyright (C) 2021-2026 Technology Matters | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU Affero General Public License as published | ||
| * by the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU Affero General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU Affero General Public License | ||
| * along with this program. If not, see https://www.gnu.org/licenses/. | ||
| */ | ||
|
|
||
| import { Twilio } from 'twilio'; | ||
| import { ConversationSID } from '@tech-matters/twilio-types'; | ||
| import { AseloCustomChannel } from '../customChannels/aseloCustomChannels'; | ||
|
|
||
| const CONVERSATION_CLOSE_TIMEOUT = 'P3D'; // ISO 8601 duration format https://en.wikipedia.org/wiki/ISO_8601 | ||
| export type CreateFlexConversationParams = { | ||
| studioFlowSid: string; | ||
| channelType: AseloCustomChannel | 'web'; // The chat channel being used | ||
| uniqueUserName: string; // Unique identifier for this user | ||
| senderScreenName: string; // Friendly info to show in the Flex UI (like Telegram handle) | ||
| onMessageAddedWebhookUrl?: string; // The url that must be used as the onMessageSent event webhook. | ||
| conversationFriendlyName: string; // A name for the Flex conversation (typically same as uniqueUserName) | ||
| twilioNumber: string; // The target Twilio number (usually have the shape <channel>:<id>, e.g. telegram:1234567) | ||
| additionalConversationAttributes?: Record<string, any>; // Any additional conversation attributes | ||
| testSessionId?: string; // A session identifier to identify the test run if this is part of an integration test. | ||
| }; | ||
| /** | ||
| * Creates a new Flex conversation in the provided Flex Flow and subscribes webhooks to it's events. | ||
| * Adds to the channel attributes the provided twilioNumber used for routing. | ||
| */ | ||
| export const createConversation = async ( | ||
| client: Twilio, | ||
| { | ||
| conversationFriendlyName, | ||
| channelType, | ||
| twilioNumber, | ||
| uniqueUserName, | ||
| senderScreenName, | ||
| onMessageAddedWebhookUrl, | ||
| studioFlowSid, | ||
| additionalConversationAttributes, | ||
| testSessionId, | ||
| }: CreateFlexConversationParams, | ||
| ): Promise<{ conversationSid: ConversationSID; error?: Error }> => { | ||
| if (testSessionId) { | ||
| console.info( | ||
| 'testSessionId specified. All outgoing messages will be sent to the test API.', | ||
| ); | ||
| } | ||
|
|
||
| const conversationInstance = await client.conversations.v1.conversations.create({ | ||
| xTwilioWebhookEnabled: 'true', | ||
| friendlyName: conversationFriendlyName, | ||
| uniqueName: `${channelType}/${uniqueUserName}/${Date.now()}`, | ||
| }); | ||
| const conversationSid = conversationInstance.sid as ConversationSID; | ||
|
|
||
| try { | ||
| const conversationContext = | ||
| client.conversations.v1.conversations.get(conversationSid); | ||
| await conversationContext.participants.create({ | ||
| identity: uniqueUserName, | ||
| }); | ||
| const channelAttributes = JSON.parse((await conversationContext.fetch()).attributes); | ||
|
|
||
| console.debug('channelAttributes prior to update', channelAttributes); | ||
|
|
||
| await conversationContext.update({ | ||
| 'timers.closed': CONVERSATION_CLOSE_TIMEOUT, | ||
| state: 'active', | ||
| attributes: JSON.stringify({ | ||
| ...channelAttributes, | ||
| channel_type: channelType, | ||
| channelType, | ||
| senderScreenName, | ||
| twilioNumber, | ||
| testSessionId, | ||
| ...additionalConversationAttributes, | ||
| }), | ||
| }); | ||
|
|
||
| await conversationContext.webhooks.create({ | ||
| target: 'studio', | ||
| 'configuration.flowSid': studioFlowSid, | ||
| 'configuration.filters': ['onMessageAdded'], | ||
| }); | ||
| if (onMessageAddedWebhookUrl) { | ||
| /* const onMessageAdded = */ | ||
| await conversationContext.webhooks.create({ | ||
| target: 'webhook', | ||
| 'configuration.method': 'POST', | ||
| 'configuration.url': onMessageAddedWebhookUrl, | ||
| 'configuration.filters': ['onMessageAdded'], | ||
| }); | ||
| } | ||
| } catch (err) { | ||
| return { conversationSid, error: err as Error }; | ||
| } | ||
|
|
||
| return { conversationSid }; | ||
| }; | ||
46 changes: 46 additions & 0 deletions
46
lambdas/account-scoped/src/conversation/patchConversationAttributes.ts
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,46 @@ | ||
| /** | ||
| * Copyright (C) 2021-2023 Technology Matters | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU Affero General Public License as published | ||
| * by the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU Affero General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU Affero General Public License | ||
| * along with this program. If not, see https://www.gnu.org/licenses/. | ||
| */ | ||
|
|
||
| import { ConversationInstance } from 'twilio/lib/rest/conversations/v1/conversation'; | ||
| import { Twilio } from 'twilio'; | ||
| import { ConversationSID } from '@tech-matters/twilio-types'; | ||
|
|
||
| // TODO: Add optimistic concurrency checks | ||
| /** | ||
| * | ||
| * @param client - Twilio client | ||
| * @param conversation - either the instance of the conversation which must have been fetched prior to calling, or the sid to fetch | ||
| * @param attributePatch - attributes to update. Will add these if they don't exist or overwrite them if they do. Setting attribute values to `undefined` or `null` will NOT remove those keys; there will still be an attribute entry for each such key. This helper only supports adding or overwriting attributes, not removing them. | ||
| */ | ||
| export const patchConversationAttributes = async ( | ||
| client: Twilio, | ||
| conversation: ConversationInstance | ConversationSID, | ||
| attributePatch: Record<string, any>, | ||
| ) => { | ||
| let conversationInstance: ConversationInstance; | ||
| if (typeof conversation !== 'object') { | ||
| conversationInstance = await client.conversations.v1.conversations | ||
| .get(conversation) | ||
| .fetch(); | ||
| } else { | ||
| conversationInstance = conversation; | ||
| } | ||
| const conversationAttributes = JSON.parse(conversationInstance.attributes); | ||
| const patchedAttributes = { ...conversationAttributes, ...attributePatch }; | ||
| return client.conversations.v1.conversations | ||
| .get(conversationInstance.sid) | ||
| .update({ attributes: JSON.stringify(patchedAttributes) }); | ||
| }; |
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
onMessageAddedWebhookUrlis documented as "onMessageSent" in the inline comment, which is misleading now that the webhook is subscribed to theonMessageAddedfilter. Update the comment to match the actual behavior (and consider renaming any remaining references to onMessageSent if they exist).