sns2telegram is a serverless message gateway built with Amazon API Gateway and AWS Lambda which forwards your incoming Amazon SNS messages to your Telegram client. This application is currently hosted in AWS SAR(Serverless App Repository) and we can easily deploy this serverless application by simply clicking Deploy from SAR.
- new a bot from botfather in Telegram with /newbot command
- copy the Access Token
Click and deploy sns2telegram from SAR to your AWS region
In the Application Settings
, enter your Access Token in the TelegramToken field.
Check I acknowledge that this app creates custom IAM roles
as this app will create a Lambda role with minimal privileges.
Click Deploy and you'll see the following screen in a few seconds.
Click View CloudFormation Stack
Click the Outputs tab and copy the BaseURL and WebHookURL.
Go to your Lambda console and search serverlessrepo-sns2telegram-SampleFunction-{RANDOM} function. Update the base_url variable with the value of BaseURL from last step. Scroll up and click Save.
OK. Our application should be all configured. Let's cURL the Telegram API to configure the webhook URL.
my_bot_token=ENTER YOUR TELEGRAM TOKEN HERE
url_to_send_updates_to=ENTER YOUR WebHookURL HERE
curl https://api.telegram.org/bot${my_bot_token}/setWebhook?url=${url_to_send_updates_to}
Response
{"ok":true,"result":true,"description":"Webhook is already set"}
Send /init to your bot and it returns your unique HTTPS URL.
Copy that URL and subscribe your Amazon SNS topic as HTTPS protocol. Paste this URL as the Endpoint.
Your Telegram will immediately receive a confirmation URL. You don't need to click that URL because Telegram client will try open this URL for preview and this will confirm the subscription for us.
OK now you are all set. All messages sending to this SNS topic will forward to your Telegram now.
$ aws sns publish --message '{"foo":"bar"}' --topic-arn arn:aws:sns:ap-northeast-1:903779448426:sns2telegram
{
"MessageId": "9f86b456-f4ef-59f0-98f7-e1942f6140a7"
}
And you receive this message in Telegram immediately:
{
"Message": {
"foo": "bar"
},
"MessageId": "9f86b456-f4ef-59f0-98f7-e1942f6140a7",
"Timestamp": "2019-08-06T01:35:10.734Z",
"TopicArn": "arn:aws:sns:ap-northeast-1:903779448426:sns2telegram",
"Type": "Notification"
}
To clean up this application, just delete the serverlessrepo-sns2telegram cloudformation stack and remove your SNS subscriptions.
We love AWS CDK! You definitely can provision sns2telegram with AWS CDK.
# go to cdk directory
cd cdk
# deploy to us-east-1
cdk sync
cdk deploy -c region=us-east-1 -c telegramToken=YOUR_TELEGRAM_TOKEN
You'll get the Outputs like this:
Outputs:
sns2telegramCdk.DynamoDBTableName = sns2Telegram
sns2telegramCdk.WebHookURL = https://2ykcmys6de.execute-api.us-east-1.amazonaws.com/Prod/telegram/cb
sns2telegramCdk.BaseURL = https://2ykcmys6de.execute-api.us-east-1.amazonaws.com/Prod/
sns2telegramCdk.HealthzURL = https://2ykcmys6de.execute-api.us-east-1.amazonaws.com/Prod/healthz
# destroy the stack
cdk destroy -c region=us-east-1
Yes, you can deploy your sns2telegram stack in one region with SNS topics in other regions.