Adjust postSurveyInit and postSurveyListener to make them work with the new bot#492
Adjust postSurveyInit and postSurveyListener to make them work with the new bot#492gpaoloni merged 13 commits intogian_CHI-175-lexfrom
Conversation
…gic, http handler and webhook)
…t survey complete
…pture control tasks
murilovmachado
left a comment
There was a problem hiding this comment.
Looks excelent, Gian! Just a few minor questions 🚀
| type HandleChannelCaptureParams = { | ||
| channelSid: string; // The channel to capture (in Studio Flow, flow.channel.address) | ||
| message: string; // The triggering message (in Studio Flow, trigger.message.Body) | ||
| language: string; // (in Studio Flow, {{trigger.message.ChannelAttributes.pre_engagement_data.language | default: 'en-US'}} ) | ||
| botSuffix: string; | ||
| triggerType: TriggerTypes; | ||
| releaseType: ReleaseTypes; | ||
| studioFlowSid?: string; // The Studio Flow sid. Needed to trigger an API type execution once the channel is released. (in Studio Flow, flow.flow_sid) | ||
| memoryAttribute?: string; // Where in the channel attributes we want to save the bot's memory (allows usage of multiple bots in same channel) | ||
| releaseFlag?: string; // The flag we want to set true in the channel attributes when the channel is released | ||
| additionControlTaskAttributes?: string; // Optional attributes to include in the control task, in the string representation of a JSON | ||
| controlTaskTTL?: number; | ||
| }; |
There was a problem hiding this comment.
I think you have this same time defined somewhere else, right?
Can't we local import types in this repo? It may work, but I'm not sure.
| const saveSurveyInHRM = async ( | ||
| postSurveyConfigJson: OneToManyConfigSpec[], | ||
| memory: BotMemory, | ||
| memory: AutopilotMemory, | ||
| surveyTask: TaskInstance, | ||
| surveyTaskAttributes: any, | ||
| hrmBaseUrl: string, |
There was a problem hiding this comment.
This means we continue saving memory (in hrm and insights) in the Autopilot format, right?
There was a problem hiding this comment.
In particular this part right here means that saveSurveyInHRM function in functions/postSurveyComplete.protected.ts will use Autopilot format, but I duplicated the same functions for HRM and Insights in /channelCapture/channelCaptureHandlers.private.ts, since the first ones will be removed soon (once we are all in Lex).
| const result = await postSurveyInitHandler(context, { | ||
| channelSid, | ||
| taskSid, | ||
| taskLanguage: taskLanguage || 'en-US', | ||
| }); |
There was a problem hiding this comment.
Just a question: Would it make sense to move the fallback en-US to Studio? I think this fallback hardcoded here wouldn't be very useful for helplines that don't operate in English, for example.
stephenhand
left a comment
There was a problem hiding this comment.
Initial look over LGTM, will probably revisit another time or 2 just to make sure I fully understand it but you're good to merge for me
| @@ -0,0 +1,531 @@ | |||
| /* eslint-disable global-require */ | |||
There was a problem hiding this comment.
I like this approach, we can extend it to be a full multicast event system in the future if we ever need to.
Description
This PR adapts the post survey flow to work properly with Lex, while also integrating it into the channel capture used for other kinds of Lex conversations.
The most relevant changes in this PR are:
functions/channelCapture.channelCapture/channelCaptureHandlersis the function where the core of this code lives. The code here is grouped like:handleChannelCapture:Since both, pre survey and post survey are captures, we need to expose at least two kinds of triggers when capturing. There are several operations that both kinds of triggers share and are essential to the channel capture, which includes:
studiotype webhooks, so the interactions with the bot don't execute the Studio Flow again.chatbotCallbackfunction as a webhook invoked on message sent. This webhooks will exchange messages between the user and the Lex bot, and will "handle the channel release" once the survey is done (next section).The two different triggers are:
withUserMessagethat takes a message already sent to the channel (usually from the child) and redirects it as it is to the Lex bot. From here user and chatbot exchange messages via webhook.withNextMessagethat takes a message that does not exists in the channel (this could be optional), sends it to the channel and only then creates the connection with the chatbot. This will leave the channel in a state where the next message will be the first to exchange with Lex, and from there the flow is the same as the above item.triggerStudioFlowrelease means once the survey is done, we'll create a new studio trigger and then send the last message from the bot. This results in the channel making it all the way into the Studio Flow. To prevent going into the same execution branch, we'll also set a "on release channel flag", e.g.preSurveyComplete, so we can make channels avoid survey if it was already completed. This is the release handler that we'll generally use for pre surveys, language selector bots or any other "information gathering survey" that needs the channel to be kept active after it completes.postSurveyCompleteis a unique kind of trigger, since this is always the last step in the life-cycle of a chat channel. Here we will save the post survey results in task attributes (insights) and in HRM.channelCapture/captureChannelWithBotfunction is just a wrapper around the above to expose the channel capture. It is intended to be called from Studio Flows primarily.channelCapture/chatbotCallbackis the webhook used to exchange messages with the Lex bots. Here we'll "release the channel" when the conversation with the bot ends, saving the bot's memory in the channel attributes and calling the appropriate release handler.Other changes in this PR include:
enable_lexfeature flag is on. Also post surveys will be triggered for custom channels now.taskrouterListeners/janitorListener, prevent the janitor cleanup the channel for custom channels if the task is a capture control task. If it's pertinent to cleanup the channel for this case, that should be done in theisCleanupBotCapturecase, notisCleanupCustomChannel.Checklist
Verification steps