_ _ _ ____ _ _
| | | | ___ _ __ ___ | | ___ _ / ___|_ _(_) __| | ___
| |_| |/ _ \ '__/ _ \| |/ / | | | | | _| | | | |/ _` |/ _ \
| _ | __/ | | (_) | <| |_| | | |_| | |_| | | (_| | __/
|_| |_|\___|_| \___/|_|\_\\__,_| \____|\__,_|_|\__,_|\___|
Concept
Heroku is git + running mechanism, running is facilited through use of Workers. The workers are basically defined using dyno hours (currency). They are of 3-4 types. Web, Webworker, UrgentWorker, Worker and stuffs like that. Nothing of huge significance, Lets launch a tg hello world bot using heroku
Pre requisites
- Git
- Python
- Vim (or your favourite editor)
- Coffee (its going to short but fun)
Start
- Create a virtualenv (very important)
python3 -m venv helloWorld
- Activate the venv,
cd helloWorld;cd /bin;source activate
(if you are on fish activate the correct respective file) - Now lets install requirements for tg bot.
pip install botogram
- Create bot token using botfather, (Simple process just message @botfather on telegram)
- Now create a python file with following code
bot = botogram.create("YOUR-API-KEY")
@bot.command("hello")
def hello_command(chat, message, args):
"""Say hello to the world!"""
chat.send("Hello world")
if __name__ == "__main__":
bot.run()
- Notice the “YOUR-API-KEY”, thats where you are supposed to put api key obtained from botfather
- Next step is simple, test it with
python helloworld.py
- Hope it works, if it shows something like this, consider it a success
INFO - Your bot is now running! INFO - Press Ctrl+C to exit.
- While its running ping your bot on telegram and see if it responds hello world.
- next step is to deal with heroku deployment, using your system package manager install heroku.
heroku login
at promp, should ask you to allow launching a browser and logging in.- Once logged in, you will be all set to “push” your code on heroku and deploy it
- Now inside the project repo,
git init
this should initialize an empty repo. - Do
git add . && git commit -m "first commit"
to stage it, please note we havent pushed our code yet. - Now the next step is to define how Heroku is supposed to cater to dependencies and how it is going to run it.
- For that, we are going to do this:
pip freeze > requirements,txt
this takes care of dependencies. - For the Execution we are going to simply define procfile,
echo "worker: python helloworld.py" > Procfile
notice there is no txt extension and also P is capital :P - Do
git add . && commit -m "Added requirement.txt and Procfile"
this should help you fix it. - Now from the same directory, do
heroku create
this will create a heroku master for you. git remote -v
should show something with heroku in its name (verification)- You can rename using
heroku apps:rename newname
if you feel like it. - Now you are ready to pus to master,
git push heroku master
this will basically push it. - Now goto heroku page on browser, then your app, then deploy and then deployment method.
- From there you can connect it with github if you want to. :P
- So as to run it,
Heroku > Heroku dashboard > Choose your app > Resources > Edit > Enable worker > Confirm
. This should start running it - you can now,
heroku logs
to see the logs and errors if any.
Note: you can create Runtime.txt for specific version of python. Note 2: Ofcourse, there is a lot mroe that can be done, just explore docs.