-
Notifications
You must be signed in to change notification settings - Fork 5
[Feature breanch] Lex chatbot integration #424
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
Conversation
|
oooooooh how exciting!!! |
|
|
||
| type EnvVars = { | ||
| CHAT_SERVICE_SID: string; | ||
| ASELO_APP_ACCESS_KEY: string; |
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.
I've meant to ask this question before in other code, but does twilio serverless set its own AWS creds? Is that why we use non-standard ENV vars?
If we could just use AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, and AWS_REGION there would be no reason to do any manual configuration of the 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.
Oh. These only come in via context, not actual ENV vars, huh? Or do they? The wording in the docs is confusing.
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.
I think ASELO_APP_ACCESS_KEY is just the name we gave to this AWS credentials, to emphasize the fact that they are not the credentials AWS_ACCESS_KEY_ID used by Github Actions, but a different that correspond to the "aselo app user" with very limited capabilities.
Is that your questions? Sorry if I misunderstood.
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.
There is one real question:
Are these "env" vars that are being loaded from context also available at process.env.ASELO_APP_ACCESS_KEY ?
And one statement:
If they are also available inside of the serverless function in the process.env object, and they are renamed in serverless to the ENV vars expected by aws-sdk (AWS_ACCESS_KEY_ID, and AWS_SECRET_ACCESS_KEY) then there is no reason to have any of the blocks like this:
AWS.config.update({
credentials: {
accessKeyId: context.ASELO_APP_ACCESS_KEY,
secretAccessKey: context.ASELO_APP_SECRET_KEY,
},
region: context.AWS_REGION,
});
aws-sdk will automatically read the env vars if they are set to the expected name.
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.
Ah I got it now. I'm not sure if this has changed, but the way to access the env vars in Twilio Serverless is vía the context object.
|
I know it's just the POC, but for the production version, could we potentially break it up into 2 completely separate studio flows managed separately in TF? Anything that breaks up the studio flow is a good thing, since it can get pretty big |
@stephenhand Yes we could do that, but I don't think it makes that much sense right now since the "second half" (after the post survey) will likely not have more than 2 widgets. Managing the terraform config seems more of a burden in this case, since we'll need to pass around Studio Flow sids around, and in any case the flows are already being split per-channel, making them more linear. Maybe @janorivera can share his opinion on this one? |
…455) * Create control task for 'pre-survey' and clean up stale chat channel * Updated expiry to 720 minutes to match AWS Lex timeout * update chatbotCallback to complete control task * isChatCaptureControl filter name correction * Removed logs and clean up branch * Correction to lowercase survey for declaring taskChannel
…re-survey_chatbot-test Chi 1967 productionize lex pre survey chatbot test
…he new bot (#492) * Modified helpers to support chatbot capture refactor * Added module channelCapture where all the code related is grouped (logic, http handler and webhook) * Adapted post survey init to new format, support changes in legacy post survey complete * Modified janitorListener to avoid shutting down channel because of capture control tasks
Description
This PR adds the require code to support migrating our pre-survey chatbot to Amazon Lex.
The bot used here can be found in AWS console. The definition for the bot was done manually via console.
This PR adds two functions:
captureChannelWithBot.protected.ts: This function is intended to be the last to be called when the Studio Flow is triggered by a new message arriving, and only branching logic/manipulation of attributes should take place before it. It is to expect that after this function is executed no task is sent to Flex. Instead the chat channel will be "captured" by the chatbot via channel webhook. Takes as parameterschannelSid,message,fromServiceUserandstudioFlowSidand it will (in order):studio, to prevent subsequent Studio Flows executions for the messages that will take place in the channel (contact and bot).onMessageSentwebhook on the channel that points to the function described in the next item.fromServiceUser(a string representing the chat service user identifier that triggered the flow) andchannelCapturedByBot(an object that containingbotId,botAliasIdandlocaleId, which are the Amazon Lex target bot identifiers,studioFlowSidwhich is the Studio Flow sid that should be triggered when the channel is "released" andchatbotCallbackWebhookSidthe sid of the webhook created in the previous step that will be removed when the channel is "released").After all this the chat channel is "captured", meaning that it is not in a Studio Flow execution, but instead will continue to react to messages via the
onMessageSentwebhook described above.webhooks/chatbotCallback.protected.ts: A channel webhook function that will be triggered ononMessageSentevents (as per above function attaching it to the "captured" channels). This webhook will act as the redirect function between the contact and the Lex bot: every new message being sent to the channel, if it's from the contact (channel attributefromServiceUser, per above function) will be redirected to the Lex bot. The messages sent back from Lex will be sent to the chat channel in order to make them visible to the contact. Lastly, if the Lex bot identified that the dialog should be closed, the webhook will delete the Lex session, cleanup the channel attributes (removingchannelCapturedByBotobject and adding the Lex gathered values asmemory), detach this same webhook from the channel and finally trigger a new API Studio Flow execution, usingchannelCapturedByBot.studioFlowSid.The last step, triggering the API Studio Flow execution is the way to "release" the channel. Here we'll re-take control of the chat channel, making it to move through the "rest of the flow after the pre survey". One important thing to note here is that the Studio Flow widgets will require some adjustments to pull the data from, as currently we rely a lot on
trigger.message.properties, which only exist in message-triggered-executions. This being an API triggered execution, will contain most of the same information underflow.data.attributes. We can add more properties to this object as needed when the execution is triggered (last step of this webhook).To make this two functions interact as expected, the following Studio flows changes are required:
After any branching logic (channel based, open hours, etc), call a
Run FunctionwidgetCapture_Channel_With_Botthat points tohttps://serverless-XXXX-production.twil.io/captureChannelWithBotwith:channelSid={{flow.channel.address}}studioFlowSid={{flow.flow_sid}}fromServiceUser={{trigger.message.From}}At this point the channel will be captured and the pre-survey will take place.

Add a REST API trigger that points to what would be "the rest of the flow" after the pre-survey resolves.

Examples of both can be found in Aselo Development Studio
Messaging Flow. To try it out you only need to changeWebchat_to_Lexwidget transition when "Equal to web" to point toCapture_Channel_With_Botwidget.Checklist
Verification steps