qusasat
May 18, 2024 · View on GitHub
A web app to show a random qusasa (quote) from the book entitled: Quotes worth burning (قصاصات قابلة للحرق) for the renowned author Dr. Ahmed Khalid Tawfik.
Included is a script (tweet_poster.py) that automatically posts a tweet with a random quote.
System requirements
- Docker
- There is no number 2!
Development
Build
docker build . -f dev.dockerfile -t qusasat:dev
Run the web app
docker run -it --rm -p 5000:5000 -v $PWD:/home qusasat:dev
Then point your browsear to http://localhost:5000 to get a random quote. The following routes are available:
- /: Return a random quote
- /.json: Return a random quote in JSON format
- /<id>: Return a specific quote by its ID
- /<id>.json: Return a specific quote by its ID in JSON format
Note that in development mode, editing and saving any file will refresh the development server automatically and you can refresh the browser to get your updates.
Production
Build
docker build . -t qusasat:prod
Run the web app
docker run -it --rm -p 5000:5000 qusasat:prod
Heroku support
The app supports Heroku out of the box.
Follow the instructions here
to deploy this web app using Heroku Container Registry.
Another way, is to deploy by pushing the master branch to the heroku remote
after configuring it. This is the standard and recommended way.
Posting tweets
Run the tweet_poster.py script after providing some environment variables:
docker run -it --rm --env-file .env qusasat:prod python tweet_poster.py
Where .env contains the following twitter tokens:
TWITTER_CONSUMER_KEY=???
TWITTER_CONSUMER_SECRET=???
TWITTER_ACCESS_KEY=???
TWITTER_ACCESS_SECRET=???
If you want to do a dry run (just print the tweets without posting them),
add an environment variable to the .env file:
DRY_RUN=1
This is useful if you want to see how long tweets will be splitted into multiple tweets in a thread.
On Heroku, you can supply those tokens using app
config vars.
To automatically send scheduled tweets, provision the
Heroku Scheduler add-on,
and set its command to: python tweet_poster.py.
Sending on Telegram
The app can send quotes to a Telegram channel. To do so, you need to create a Telegram bot and channel, and get the bot token and the channel ID. Then, provide the following environment variables:
TELEGRAM_CHAT_ID=???
TELEGRAM_BOT_TOKEN=???
MESSAGES_SIGNATURE=???
Then, run the telegram_sender.py script:
docker run -it --rm --env-file .env qusasat:prod python telegram_sender.py
Message signatures are optional and can be used to add a signature to the end of each message.
To generate a token for the bot and identify the channel id, check the official docs for python-telegram-bot.
Deploy using AWS Lambda
A Github Action is provided to deploy the app to AWS Lambda.
It is triggered on push to the master branch.
To deploy manually do the following:
-
Install node 19
-
Install Serverless framework:
npm install -g serverless -
Install dependencies:
npm install -
Configure AWS credentials:
aws configure -
Deploy the app:
TELEGRAM_BOT_TOKEN=<bot_token> \ TELEGRAM_CHAT_ID=<chat_id> \ MESSAGES_SIGNATURE=<signature> \ sls deploy --aws-profile <profile_name> --stage prod