https://github.com/icelam/see-this-and-drink-water-telegram-bot
「見字飲水 Telegram Bot」會喺每日嘅香港時間上午10點至夜晚9點每個鐘提你飲一次水。 "See This and Drink Water Telegram Bot" will remind you to drink water every hour from 10 AM. through 9 PM.
https://github.com/icelam/see-this-and-drink-water-telegram-bot
aws-lambda aws-lambda-python serverless-framework telegram-bot telegram-python telegram-python-bot telegram-scheduled-message
Last synced: 23 days ago
JSON representation
「見字飲水 Telegram Bot」會喺每日嘅香港時間上午10點至夜晚9點每個鐘提你飲一次水。 "See This and Drink Water Telegram Bot" will remind you to drink water every hour from 10 AM. through 9 PM.
- Host: GitHub
- URL: https://github.com/icelam/see-this-and-drink-water-telegram-bot
- Owner: icelam
- Created: 2021-10-18T16:24:24.000Z (about 4 years ago)
- Default Branch: develop
- Last Pushed: 2023-11-01T12:32:45.000Z (about 2 years ago)
- Last Synced: 2025-08-27T06:46:59.766Z (2 months ago)
- Topics: aws-lambda, aws-lambda-python, serverless-framework, telegram-bot, telegram-python, telegram-python-bot, telegram-scheduled-message
- Language: Python
- Homepage:
- Size: 43.9 KB
- Stars: 0
- Watchers: 1
- Forks: 2
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
# 見字飲水 Telegram Bot (See This and Drink Water Telegram Bot)
> 「見字飲水 Telegram Bot」會喺每日嘅香港時間上午10點至夜晚9點每個鐘提你飲一次水。
>
> "See This and Drink Water Telegram Bot" will remind you to drink water every hour from 10 AM. through 9 PM.
## Development Notes
### Tech Stack
* Python 3
* [Serverless](https://www.npmjs.com/package/serverless)
* AWS Lambda
* AWS CloudFormation - Used by Serverless when doing deployment
* AWS CloudWatch - Automatically Setup by Serverless when doing deployment
* AWS S3 - Used by Serverless when doing deployment
> **Note:**
> Both Lambda, CloudWatch, and CloudFormation has free tier provided. However for AWS S3, it has only has free tier for limited time.
>
> To check the tier limit, head to [https://aws.amazon.com/free/](https://aws.amazon.com/free/). It is recommended to setup a billing alert by following the documentation on [https://docs.aws.amazon.com/awsaccountbilling/latest/aboutv2/tracking-free-tier-usage.html](https://docs.aws.amazon.com/awsaccountbilling/latest/aboutv2/tracking-free-tier-usage.html).
### Development Setup
#### Prerequisites
* Node.js 14 installed
* Python 3 installed
* `virtualenv` installed
#### Prepare `serverless` environment
##### NPM
```bash
npm install serverless@3 --global
npm install
```
##### Yarn
```bash
yarn global add serverless@3
yarn
```
#### Setup Python Virtual Environments
```bash
# Create a new virtual environment
virtualenv venv --python=python3
# Activate virtual environment created
. venv/bin/activate
# Install dependencies listed in requirements.txt
pip install -r requirements.txt
```
#### Setup Bot to send message
Save a copy of `.env` and name it as `.env.local`, place your Telegram bot token and Telegram Chat ID in the configuration file.
> **Note:**
> Environment variables are used instead of `ConfigParser` provided by Python since [by default AWS Lambda will encrypt environment variables using KMS](https://docs.aws.amazon.com/whitepapers/latest/kms-best-practices/encrypting-lambda-environment-variables.html), and we don't want to store tokens using plain text.
##### 1. How to get a Telegram bot token
1. In Telegram, search for the user `@BotFather`.
2. Use the command `\newbot` and choose a name and username for your bot.
3. `@BotFather` will return you the token of the bot created. Remember to keep it safe!
##### 2. How to get a Telegram chat ID
1. Send a `/start` command to the telegram bot created in the previous step
2. Visit `https://api.telegram.org/bot/getUpdates`
3. Look at the API response, `result[0]['message']['chat']['id']` should contains ID of the chat. Remember to copy the `-` prefix if exists.
##### 3. Test if Bot token and Chat ID is correct or not
Open Terminal, run the following command. You will need to replace `` and `` with the one you get in previous steps.
```bash
curl -X POST "https://api.telegram.org/bot/sendMessage" -d "chat_id=&text=Hello World"
```
#### Deployment
##### Create named AWS profile (for first time deployment)
1. Create new user on AWS, with "Programmatic access"
2. Assign permission to user by "Attaching existing policies directly"
* In the official Serverless blog, they have introduced [serverless-policy generator](https://github.com/dancrumb/generator-serverless-policy) to help you generate IAM policies.
3. Configure new AWS credientials to use newly created user
```bash
aws configure --profile
```
##### Deploy to Lambda
```bash
# Load environment variables
. load-environment-variables.sh
# Deploy to AWS Lambda using serverless cli
serverless deploy --aws-profile
```
#### Settings
##### Add / Edit bot messages
All messages are stored in `app/messages.txt`, and will be randomly picked when handler is triggered. To update the message sets, simply edit and save the file. Re-deployment is needed for changes to take effect.
##### Add / Edit stickers
All sticker IDs are stored in `app/stickers.txt`, and will be randomly picked when handler is triggered. To update the sticker sets, you will need to get the sticker ID by sending stickers to the created bot. Visit `https://api.telegram.org/bot/getUpdates`, stickers ID can be found in the API reponse node `result[n]['message']['sticker']['file_id']`. Re-deployment is needed for changes to take effect.
##### Edit bot schedule
Open `serverless.yml`, edit `functions['cron']['schedule']`. Syntax reference: [https://docs.aws.amazon.com/lambda/latest/dg/services-cloudwatchevents-expressions.html](https://docs.aws.amazon.com/lambda/latest/dg/services-cloudwatchevents-expressions.html)
#### Test
##### Testing the handler
Run the following command inside virtual environment:
```bash
# Load environment variables
. load-environment-variables.sh
# Execute handler
serverless invoke local -f cron
```
#### Others
##### Delete deployed Lambda function
```bash
serverless remove --aws-profile
```