Building a Telegram bot on AWS Lambda to manage my spam folder
Somewhere along the way my email address ended up on a spam list, a really annoying spam list that has thousands of random domain names like ifjeoi423.gq
and a list of trending keywords like Trump
, brexit
, iPhone
and the like. This makes it extremely difficult to consistently block them, I also often get up to 90 emails a day. Luckily they are caught and put in my spam folder, but it quickly piles up with useless rubbish that needs clearing out.
I wanted an easy way to automate clearing all these emails out of my spam folder, so I decided to write a Telegram bot to let me do it.
I first had to log all the network requests that were involved in clearing that spam folder via my email provider's UI, this was fiddly but I won't go into it as its very domain specific!
I previously had another version of this bot running on a computer running at my house but it needed restarting whenever I would restart the machine or need the power socket back!
I decided to offload all of this to the cloud. I didn't need much processing power and the bot also doesn't need to be constantly on and polling (I had the bot in polling mode before where it would constantly ask Telegram if there was a new message - not very efficient!). These requirements describe a very common AWS service: Lambda. I would also need AWS API Gateway to allow me to invoke the Lambda via an HTTP POST
request (a webhook).
Luckily Serverless does all the heavy lifting for me here in terms of provisioning these resources. I just give it the following config file along with an AWS access key and AWS secret key for a specially permissioned IAM user.
In the config file I define the provider, environment, environment variables and the function itself and then I can deploy with Serverless and have it package the Lambda, upload it to S3, create an API Gateway entry to allow HTTP traffic to invoke it and finally setup the CloudWatch logs so I can monitor the Lambda. ✨ Magic ✨
Now that I've talked about it, lets run through the nitty gritty of getting this service from code to deploy.
To interact with Telegram I'm using Telegraf, a fantastic Telegram API library for Node.js. The handler code is really simple and I've abstracted the actual spam clearing to another file as its not relevant to this post.
With the newly out of beta GitHub Actions any pushes to the repo will trigger a run of my GitHub Actions config file, which installs the dependencies, then uses Serverless to upload the handler and provision all the infrastructure (a great thing about Serverless is that it can also decomission all that infrastructure very cleanly and leave you with no remnnants on your AWS profile).
Unfortunately its not all automated. Becuase this is a webhook based Telegram bot, I need to tell Telegram to call a URL when it recieves an event from the chat (this only has to be provided the first time I deploy). The URL in question is the AWS API Gateway URL that is created when Serverless sets up the infrastructure and is found in the output of the GitHub Action I setup earlier, it'll be something like https://randomCharacters.execute-api.region.amazonaws.com/prod/path
. Once you have this URL you need to assosiate this webhook with the bot by sending a request to Telegram's setWebhook
API
Now you're ready to try it out in Telegram! You can read all the Telegram docs to setup your bot at this link