Friday, 8 May 2020

Creating and uploading a simple telegram bot with Pythonanywhere.com

In this post we are going to see how we can create a simple telegram app and deploy it, using pythonanywhere.com.

 

pythonanywhere.com is very handy for deploying small and mid-scale projects, especially projects like tweeter bot or telegram bot.

 

So, here I am not planning to go deep into telegram bot tutorial, as there are already so many amazing tutorials.

 

We would be using code from this tutorial:

https://www.freecodecamp.org/news/learn-to-build-your-first-bot-in-telegram-with-python-4c99526765e4/

 

Code is very easy to understand. I have kept entire code here for your reference as well. You just need to provide your telegram bot key. Run this on your local machine and test it!


from telegram.ext import Updater, InlineQueryHandler, CommandHandler

import requests

import re

 

def get_url():

    contents = requests.get('https://random.dog/woof.json').json()   

    url = contents['url']

    return url

 

def get_image_url():

    allowed_extension = ['jpg','jpeg','png']

    file_extension = ''

    while file_extension not in allowed_extension:

        url = get_url()

        file_extension = re.search("([^.]*)$",url).group(1).lower()

    return url

 

def bop(bot, update):

    url = get_image_url()

    chat_id = update.message.chat_id

    bot.send_photo(chat_id=chat_id, photo=url)

 

def main():

    updater = Updater('YOUR_TOKEN')

    dp = updater.dispatcher

    dp.add_handler(CommandHandler('bop',bop))

    updater.start_polling()

    updater.idle()

 

if __name__ == '__main__':

    main() 

    

Now, how to upload this on pythonanywhere.com?

Create and verify a free account on pythonanywhere.com.

 Go to dashboard, here you will find most of the things you can do in python anywhere. Let me know if I should write tutorial on that.


Now first we want to upload our telegram_bot python file. Select “Browse Files”. Then upload files.

Once you upload the file it should look like this:



 Now we need to run this file, go to console, and start a new Bash console. (click on Bash)




 

 

This will start a completely new console for you, let us run “ls -la” command and then try to run our bot file using “python3 telegram_example.py”.

 


But you will see that we have not installed “python-telegram-bot” module.

Let’s do : pip3 install python-telegram-bot.

 



It’s giving us error: Consider using the `--user` option or check the permissions.

So basically, you need to do:

pip3 install --user python-telegram-bot

details are given at :  https://help.pythonanywhere.com/pages/InstallingNewModules/

 

Now we are set to run our bot, type “python3 telegram_example.py”. it should show as follows:

 


 

Now you can test your bot in your telegram app by sending command : /bop . If you face any issue check your telegram bot key . Or you are welcome to comment down.

 

Advice / Caution : even though this should be running your bot forever , but some time I have seen pythonanywhere just shutdown your console , and your bot won’t be available . So if you want a reliable option you should opt for their 5$ plan , as they provide a always ON task there.