Building a Telegram Bot with AWS API Gateway and AWS Lambda
Hello, connections..!!! Hope you are doing well. In this article, I am going to explain how to build a telegram bot using AWS API gateway and lambda.
Services Used:-
1.) Lambda
2.) API Gateway
3.) Cloud Watch
Lambda Function:- AWS Lambda is a serverless compute service that runs your code in response to events and automatically manages the underlying compute resources for you. These events may include changes in state or an update, such as a user placing an item in a shopping cart on an e-commerce website. You can use AWS Lambda to extend other AWS services with custom logic or create your own backend services that operate at AWS scale, performance, and security. AWS Lambda automatically runs code in response to multiple events, such as HTTP requests via Amazon API Gateway, modifications to objects in Amazon Simple Storage Service (Amazon S3) buckets, table updates in Amazon DynamoDB, and state transitions in AWS Step Functions.
API Gateway:- Amazon API Gateway is a fully managed service that makes it easy for developers to create, publish, maintain, monitor, and secure APIs at any scale. APIs act as the "front door" for applications to access data, business logic, or functionality from your backend services. Using API Gateway, you can create RESTful APIs and WebSocket APIs that enable real-time two-way communication applications. API Gateway supports containerized and serverless workloads, as well as web applications.
CloudWatch:- Amazon CloudWatch monitors your Amazon Web Services (AWS) resources and the applications you run on AWS in real-time. You can use CloudWatch to collect and track metrics, which are variables you can measure for your resources and applications.
The CloudWatch home page automatically displays metrics about every AWS service you use. You can additionally create custom dashboards to display metrics about your custom applications and display custom collections of metrics that you choose.
Here are the steps to be followed to create a telegram bot with AWS API gateway and Lambda:-
Setting up telegram bot:-
1.) Go and search for BotFather in Telegram and click on start.
2.) Select Create a new bot and give a name to your bot.
3.) Then your bot is ready and keep the HTTP API token with you.
Creating a Lambda Function:-
1.) Name the lambda function and select Python 3.7 as runtime.
2.) Click on Create a Sample function.
Integration with API Gateway:-
1.) Go to API Gateway and Select build on HTTP API.
2.) Select the Lambda function that you have created.
3.) Click on Next.
4.) In configuring route events select the POST method.
5.) Click on next and create an API Gateway.
6.) Copy the API endpoint and resource path.
7.) Now set up the webhook by using the HTTP API token.
8.) So our webhook is set.
Lambda Function:-
1.) Now in the lambda function you can see that the API gateway is triggered.
2.) Now paste the HTTPS token in the url of the lambda function code.
3.) Now deploy the Lambda code.
Recommended by LinkedIn
Code:-
import json
import requests
def lambda_handler(event, context):
print(event)
try:
body=json.loads(event['body'])
print(body)
message_part=body['message'].get('text')
print("Message part : {}".format(message_part))
data = {'url': message_part}
payload=requests.post('https://cleanuri.com/api/v1/shorten',...)
short_url=payload.json()['result_url']
print("The short url is : {}".format(short_url))
chat_id=body['message']['chat']['id']
url = f'https://api.telegram.org/bot{HTTP Token}/sendMessage'
payload = {
'chat_id': chat_id,
'text': short_url
}
r = requests.post(url,json=payload)
return {
"statusCode": 200
}
except:
return {
"statusCode": 200
}
Testing our bot:-
1.) Now give a long URL to our bot telegram bot.
2.) Then it will give the shortened URL.
CloudWatch:-
You can monitor all the actions in the cloud watch.
So, now you can easily build a bot and shorten your long URLs to small ones by using AWS Lambda and API Gateway.
Keep it up