Write a Chatbot in Swift and Deploy to AWS Lambda
Tags: ios, swift, aws, beginners
Published: 2020-08-05 15:33:29

---
title: Write a Chatbot in Swift and Deploy to AWS Lambda
published: true
description: 
tags: ios, swift, aws, beginners
canonical_url: https://getstream.io/blog/swift-lambda-chat-bot/
---

Did you know you can use Swift in the backend to build a chatbot and deploy it to AWS? We've recently published an Open Source project called [Swift Lambda](https://github.com/getstream/swift-lambda) to make the process easier.

In this tutorial, we'll use Swift Lambda to build a chatbot that can reply to user messages automatically using [Stream's powerful chat API](https://getstream.io/chat/).

![Animation shows a Swift Lambda being deployed and a chatbot interaction happening in iPhone simulator](https://i.imgur.com/f5VTHVN.gif)

The full code for this tutorial can be found in the [Samples folder inside the Swift Lambda repository](https://github.com/GetStream/swift-lambda/tree/main/Samples)

## Requirements
- [Set up a Chat Application](https://getstream.io/blog/inapp-support-chat-ios/)
- [Set up Swift Lambda](https://getstream.io/blog/swift-lambda-aws-serverless/)

## Set the Webhook URL

After you've set up your iOS chat application and your Swift Lambda is up and running, you need to configure the Webhook URL in the Stream Chat dashboard.

![Screenshot shows Webhook URL is configured in the Stream Chat dashboard](https://i.imgur.com/SySsyor.png)

As you can learn in the [webhooks docs](https://getstream.io/chat/docs/webhook_events/?language=swift), every event will generate a `POST` request to the provided endpoint, which is where our Swift Lambda is running. To reply to user messages, we need to parse this request.

## Configure Swift Lambda

Initially, Swift Lambda is set up to handle `GET` requests. 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`.

## Parse the request data

When the `POST` request hits your lambda, it will contain a JSON object describing the event. For this chatbot, we're only interested in the `message.new` event. To see which fields you can expect in this event, see the [documentation](https://getstream.io/chat/docs/webhook_events/?language=swift#codemessagenewcode).

In the `main.swift` file, paste the code below.

{% gist https://gist.github.com/cardoso/f47efb9a508fc9fa8fa61a20ee7eb6d9 %}

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. We also check if it's a "message.new" event and if the id is not the same as the bot. The last check will prevent an infinite loop of sending a message as the bot and receiving an event for that new message. 

We'll define the `botReply` function in the next step.

## Send reply to chat

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. To post a message, we'll use [Stream Chat's REST API endpoint for sending messages](https://getstream.io/chat/docs_rest/#send-message).

{% gist https://gist.github.com/cardoso/445cb692d8662cb7a209ca5a99410b51 %}

That code accesses the REST API using the good old URLSession. Just remember to include the imports from the previous snippet and replace the API key and JWT. You can generate a JWT with your secret in the [jwt.io](https://jwt.io/) site.

## Deploying and testing the chatbot

One of the main perks of using [Swift Lambda](https://github.com/GetStream/swift-lambda) is that it makes iteration fast and easy. After writing your chatbot code, all you need to do is run the `./Scripts/deploy.sh` script again and wait a few seconds.

After the deployment is done, you can run your chat app and type something in a channel with the bot.

![Image shows a chat UI running on iPhone simulator with a user and a bot chatting](https://i.imgur.com/wJWU86t.png)

## Next steps with the chatbot

This chatbot is very simple and made to demonstrate the usage of Swift Lambda to access the Stream Chat API. However, by [pairing Stream's powerful chat features with AI services such as Google's Dialogflow](https://getstream.io/blog/adding-chatbots-to-your-stream-chat-app-using-googles-dialogflow/) or [Amazon's Lex](https://aws.amazon.com/lex/), you can provide your users with a beneficial AI chat experience in the shortest amount of development time.