Flask service on Heroku for Telegram bot
- Please follow the format in
menu.csv.sample. - The date must follow this format
dd/mm/yyyy. E.g.12/1/2018for January 12, 2018 and25/12/2018for December 25, 2018. - All characters can be of lower case. The bot will capitalize each column entry.
- Be extra careful about the format to specify multiple dishes. Refer to the CSV file.
- Download the script via
git cloneor a simpleSave As. - Put
menu.csvinto the same directory for simplicity. - Install Python 3 (comes with
pip).
Assuming upload.py and menu.csv are in the current directory, run the following commands:
pip3 install psycopg2
export DB_URL=(yourDatabaseUrl)
python3 upload.py menu.csvRefer to hungry-ke-bot Wiki for both instructions and reference screenshots.
Instructions:
- Install Python 3.6.4
- Press the Windows icon on your keyboard to search Windows
- Type "environment" in the search field
- Go to "Edit environment variables for your account"
- Click "New" under "User variables for (yourUsername)"
- Set "Variable name" to "DB_URL" and "Variable value" to (yourDatabaseUrl)
- Open a new command prompt window and run the following commands:
cd Documents\hungry-ke-bot # Or wherever this folder is inside your computer
pip install psycopg2
python upload.py menu.csvYou should observe the rows in your CSV file being printed to the terminal.
Please delete from the database manually (i.e. DELETE FROM menu_items) and do the upload procedure with the updated CSV file.
brew install python3 # Linux has different package managers (e.g. apt, yum)
git clone https://github.com/jia1/hungry-ke-bot.git
cd hungry-ke-bot
python3 -m venv .
. bin/activate
pip3 install -r requirements.txt
# Run the following commands if the migrations directory is not available
# We need to commit the output of these commands because Heroku only offers 2 free dynos,
# and we will assign them to the web and upgrade command (see Procfile)
python3 manage.py db init
python3 manage.py db migrate
# If you want to run a local server, you need to export the following environment variables:
# BOT_TOKEN, DB_URL, FLASK_APP=app.py, SENTRY_DSN
# And then you run the following commands:
python3 manage.py db upgrade- Create a Heroku application.
- Define the following environment variables:
BOT_TOKEN,DB_URL,FLASK_APP=app.py,SENTRY_DSN(under the Settings tab). - Push repository and deploy to Heroku.
- Switch on the
upgradedyno atConfigure dynos(under the Resources tab).
- Access
https://api.telegram.org/bot(yourToken)/setWebhookin your browser (replace yourToken with your bot token). - Access
https://api.telegram.org/bot(yourToken)/setWebhook?url=https://(someEndpoint)in your browser (someEndpointwould be something likehttps://my-heroku-app.herokuapp.com/get-todays-menudepending on your Heroku app).
Refer to the following if still unsure: how to use setwebhook in telegram? - Stackoverflow
I am not sure what is the best way to do this, but this is how I do it:
- Drop the
alembictable - Delete the
migrationsdirectory - Re-run
python3 manage.py db initandpython3 manage.py db migrate - Push changes