In this post, I’ll walk you through how to build a WhatsApp chatbot on AWS using one of Facebook’s official WhatsApp partner’s API. This gave rise to WhatsApp chatbots, which helps huge businesses to serve their customers. If everything works out properly, you should see notifications in your channel. This allows businesses to leverage whatsApp’s platform to offer better service for their customers. There are many more for CodePipeline which you can find in the AWS documentation. You’re not limited to the events which I used. resource "aws_cloudformation_stack" "chatbot" Next, we can use our template and fill in our variables.
roleArn: Type: String logLevel: Type: String slackChannelId: Type: String slackWorkspaceId: Type: String snsTopicArn: Type: CommaDelimitedList Resources: SlackChannelConfiguration: Type: AWS::Chatbot::SlackChannelConfiguration Properties: ConfigurationName: !Ref configurationName IamRoleArn: !Ref roleArn LoggingLevel: !Ref logLevel SlackChannelId: !Ref slackChannelId SlackWorkspaceId: !Ref slackWorkspaceId SnsTopicArns: !Ref snsTopicArn AWSTemplateFormatVersion: "" Description: AWS Chatbot Configuration Parameters: configurationName: Type: String Description: The name of the configuration. Setting up the CloudFormation Template for AWS ChatbotĪs a starter, we need to define our CloudFormation template as YAML in a separate file aws_chatbot_template.yaml. We’ll set up everything via Terraform & CloudFormation. Scripts/deploy.sh script again and wait a few seconds.Īfter the deployment is done, you can run your chat app and type something in a channel with the bot.Integrating AWS Chatbot via SNS, Lambda & Slack After writing your chatbot code, all you need to do is run the. One of the main perks of using Swift Lambda is that it makes iteration fast and easy. You can generate a JWT with your secret in the jwt.io site. Just remember to include the imports from the previous snippet and replace the API key and JWT.
CHATBOT AWS CODE
That code accesses the REST API using the good old URLSession. To post a message, we'll use Stream Chat's REST API endpoint for sending messages. Now that we're aware of when a message is sent to a channel, and we know it's not a message from the bot itself, we can trigger a response from the bot. We'll define the botReply function in the next step. The last check will prevent an infinite loop of sending a message as the bot and receiving an event for that new message. We also check if it's a "message.new" event and if the id is not the same as the bot. That code will parse the JSON body into a dictionary to extract the parameters we need: the channel type and id in which the event happened. In the main.swift file, paste the code below. To see which fields you can expect in this event, see the documentation. For this chatbot, we're only interested in the message.new event. When the POST request hits your lambda, it will contain a JSON object describing the event. We need to change it to process POST requests, which can be done by editing the serverless.yml file and swapping the line method: get with method: post. Initially, Swift Lambda is set up to handle GET requests. As you can learn in the webhooks docs, every event will generate a POST request to the provided endpoint, which is where our Swift Lambda is running.