Example of usage of AwS Lambda to administrate remote IoT device using Telegram bot using AWS IoT Example.
This code use Serverless framework refere to official documentation.
Install the Serverless cli
npm install -g serverless
Install project dependencies
npm install
Create e new .env
file based on .env.dist
in the project root as the follow
Add profile name used in AWS cli,
you can find the configurations in ~/.aws/credentials
file.
export AWS_PROFILE=
export AWS_REGION=
Configure Telegram Bot creating and retrieve a token asking to BotFather. Set the admin username and notification chatId to receive notification and send command.
export API_TOKEN=
export ADMIN_USERNAME=
export NOTIFICATION_CHAT=
Add some environment configurations like an S3 bucket name used to send image and the IoT hostname.
export PHOTO_BUCKET=
export IOT_HOST=
Set message string
export MSG_MOTION_ACTIVATED=Motion detected
export MSG_MOTION_DEACTIVATED=Motion alarm released
export MSG_NOT_A_COMMAND=Invalid Command
export MSG_COMMAND_NOT_FOUND=Command not found
export MSG_WELCOME=Welcome
export MSG_ACK=Ok
Use deploy
make command to create the environment on AWS
make deploy
Once deployed set the Telegram Bot Webhook as the telegramCommand
Lambda API Endpoint executing a simple HTTP request
curl https://api.telegram.org/bot<bot token>/setWebHook?url=<telegramCommand endpoint>
Bot slash commands are declared in command.js
file, the provide example contain
/start
Simply the welcome message
/temp
Get DHT11 temperature value
/hum
Get DHT11 humidity value
/buzz
Make buzzer ring
/photo
Take a photo using camera
/alarm on
Enable motion alarm
/alarm off
Disable motion alarm
/led on
Switch on led
/led off
Switch off led
You can read the remote Lambda log using make commands log-motion-activated
, log-motion-deactivated
, log-command
, log-photo
.