Go Serverless Amazon Refbot

This project allows you to run a bot on the Amazon Lambda serverless platform to generate referral links.
Configuration
Getting a Bitly API key
- Go to Bitly's website bitly.com and login or create an account.
- Click on your account name in the top right and choose
Group settings.
- Next up, choose
Advanced settings and click on API support.
- In the second paragraph you'll find a link to
User settings, click on it.
- Input your password and click on
Generate token at the bottom.
- Save this token, we'll need it later.
DynamoDB configuration
- Go to DynamoDB's web page: https://console.aws.amazon.com/dynamodb/
- Press the "Create Table" button in the top of the page.
- Create the
Users table and use TelegramID as the partition key, Number type (leave "use default settings" ticked).
- Create the
Requests table and use XID as the partition key, String type (again, leave "use default settings" ticked).
- Depending on your use, you may want to turn off the provisioning for the tables.
IAM configuration
- Go to IAM's web page: https://console.aws.amazon.com/iam
- Press on
Roles in the menu on the left.
- Press the
Create role button.
- Choose
Lambda from the list and press the next button.
- Choose the
AWSLambdaBasicExecutionRole role from the list.
- Press the
Next button.
- Input the tags you want or leave blank if you don't need them.
- Press again the
Next button.
- Input the role name in the form and press the
Create role button.
- Choose the role you just created from the list.
- Press the
Add inline policy text on the right of the screen.
- Click the
Choose service text and choose DynamoDB.
- Select the following operations:
- Read
- BatchGetItem
- ConditionCheckItem
- GetItem
- Query
- Scan
- Write
- Click
Resources, find the Table entry and click Add ARN.
- Fill in the ARN with the ARNs of the tables you created earlier, you will find them at the bottom of the table page.
- Do the same thing for the
Index fields: ARN/index/theIndexesWeChoseEarlier.
- Press
Verify policy.
- Name the policy.
- Create the role.
Lambda function creation
- Go to Lambda's web page: https://console.aws.amazon.com/lambda
- Press
Create function on the top right.
- Choose
Author from scratch.
- Name your function and choose the
Go 1.x runtime.
- In the execution roles, choose
Use an existing role and choose the one we created earlier.
- Once the function has been created, fill in the following environment variables:
AMAZON_DOMAIN: the domain for which you want to use the bot (e.g. amazon.it).
BITLY_KEY: your Bitly API key.
REF_ID: your referral id from the Amazon affiliates program.
REQUEST_TABLE_NAME: the name you gave to the Requests table.
TG_KEY: a bot token from Telegram's BotFather.
USER_TABLE_NAME: the name you gave to the Users table.
- Write
main as the function handler.
API Gateway configuration
- Go to the API Gateway's web page: https://console.aws.amazon.com/apigateway
- Go to API and choose
Create API.
- Choose
New API and use a Regional endpoint.
- Click on the newly created API and, from the dropdown
Actions menu, choose Create Method.
- Choose the
POST method and confirm by pressing on the tick.
- Make sure that
Lambda function is selected as the Integration type.
- Make sure that
Lambda Proxy Integration is disabled.
- Choose the appropriate region and write name of the function you've created in the
Lambda function field.
- Make sure that in the
Body mapping templates of the function, When there are no templates defined (recommended)" is selected.
- Deploy the API by choosing the option from the dropdown menu. This way you'll be given the URL we'll use to set up the bot's webhooks.
Compiling
Now that we have (finally) set everything up, we can compile. To do so, we need to get Amazon's Go SDK with
go get -u github.com/aws/aws-sdk-go
Linux
To compile on Linux we need to run:
GOOS=linux go build main.go
zip function.zip main
Windows
On Windows, instead:
set GOOS=linux
go build -o main main.go
%USERPROFILE%\Go\bin\build-lambda-zip.exe -o main.zip main
I have included the buildzip.bat script that does exactly this.
You can now upload the function via the web interface and save the changes.
Webhook setup
From the Lambda page, get the API Endpoint and from Telegram your bot token.
Perform the appropriate CURL request.
Webhook creation
curl --request POST --url https://api.telegram.org/bot<BOT-TOKEN>/setWebhook --header 'content-type: application/json' --data '{"url": "<API-GATEWAY-URL>"}'
Webhook deletion
curl --request POST --url https://api.telegram.org/bot<BOT-TOKEN>/setWebhook --header 'content-type: application/json' --data '{"url": ""}'
Disclaimer
This project is meant for educational purposes.
I am not responsible for possible breaches of Amazon Affiliates's ToS, accidental charges from Amazon Web Services, etc.
By using this software, you take all the responsibilities.