Running a Serverless Telegram Bot from AWS Lambda

{ 
"name":"Test Name",
"email":"example-email@email.com",
"message":"This is an example message"
}
import jsondef lambda_handler(event, context):
name = event['name']
email = event['email']
message = event['message']
telegram_msg =
f'From: {name}\nEmail: {email}\n Message:{message}'

return {
'statusCode': 200,
'body': json.dumps(telegram_msg)
}
{
"name": "",
"email": "",
"message": ""
}
{}
{
“name”: “ “,
“email”: “ “,
“message”: “ “
}
{
"name": "FirstName LastName",
"email": null,
"message": "Example message!"
}
def check_param(event:dict, param:str, field_errors:dict)->str:
# checks for key and if val is null or an empty string
if param in event and event[param] and str(event[param]).strip():
return str(event[param])
else:
field_errors[param] = "Required"
return None
def lambda_handler(event, context):
field_errors = {}
# ignore event parameters other than these 3
name = check_param(event, 'name', field_errors)
email = check_param(event, 'email', field_errors)
message = check_param(event, 'message', field_errors)

if field_errors:
raise Exception(json.dumps({'field_errors': field_errors}))

telegram_msg = f'From: {name}\nEmail: {email}\nMessage:{message}'
return {
'statusCode': 200,
'body': json.dumps(telegram_msg)
}
import os...chat_id = os.environ['MY_CHAT_ID']
telegram_token = os.environ['TELEGRAM_BOT_TOKEN']
from botocore.vendored import requests...api_url = f"https://api.telegram.org/bot{telegram_token}/"

params = {'chat_id': chat_id, 'text': telegram_msg}
res = requests.post(api_url + "sendMessage", data=params).json()
if res['ok']:
return {
'statusCode': 200,
'body': res['result'],
}
else:
print(res)
return {
'statusCode': 400,
}
import json
import os
from botocore.vendored import requests
def check_param(event:dict, param:str, field_errors:dict)->str:
# checks for key and if val is null or an empty string
if param in event and event[param] and str(event[param]).strip():
return str(event[param])
else:
field_errors[param] = "Required"
return None
def lambda_handler(event, context):
field_errors = {}
# ignore event parameters other than these 3
name = check_param(event, 'name', field_errors)
email = check_param(event, 'email', field_errors)
message = check_param(event, 'message', field_errors)

if field_errors:
raise Exception(json.dumps({'field_errors': field_errors}))

telegram_msg = f'From: {name}\nEmail: {email}\nMessage {message}'


chat_id = os.environ['MY_CHAT_ID']
telegram_token = os.environ['TELEGRAM_BOT_TOKEN']

api_url = f"https://api.telegram.org/bot{telegram_token}/"

params = {'chat_id': chat_id, 'text': telegram_msg}
res = requests.post(api_url + "sendMessage", data=params).json()


if res["ok"]:
return {
'statusCode': 200,
'body': res['result'],
}
else:
print(res)
return {
'statusCode': 400,
'body': res
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store