Step-by-step Guide on Using Telegram Bots

Step-by-step Guide on Using Telegram Bots

Nicci Mende, Maria Spark

Here at the Idea Factory, we hear countless pitches every single day of the week. From rocket ships to the moon to plastic-free packaging, we’ve heard an incredible array of ways to improve any and every industry. 

The majority of our most exciting business plan ideas are based on Telegram bots. And, why do we love these bot startups? First of all, they are affordable. There is virtually no overhead to creating a business bot. Secondly, they are easy to create in a short time. 

When you see the diverse array of problems that a bot can solve, it is truly incredible. 

Creating a Business out of a Bot

Take, for example, internet usage in Africa. Did you most of Africa's data is used on messaging apps? Especially in the Sub Saharan region of Africa, for many people, the internet is WhatsApp (though this is changing quickly to Telegram in many countries). 

Africans’ preferred messaging platform is not one of many ways to get online, it’s the whole internet. Many Africans don’t use a traditional browser like Chrome or Firefox, but instead rely on a handful of apps. This is in part because the internet is primarily accessed through mobile phones rather than computers. 

One of the best ideas recently presented at the Idea Factory is a startup based on a WhatsApp chatbot. This allows users to access the internet via WhatsApp. For Sub Saharan Africans who spend 100% of their online time on WhatsApp, this provides a whole new world of content and information. 

Telegram Based Startups

When it comes to startup concepts, if you think that your product would be useful, there is a good chance someone else will too. While that’s all well and good for expensive products that are difficult to develop and produce, it’s a different story with Telegram bots. 

If you have a great idea that can be achieved with a Telegram bot, it’s a call to action. This is a startup that you can launch in a matter of days. Even if you’re not a professional app developer, you can have a useful and usable product ready faster than nearly any other type of business. 

Two other incredible startups developed in the Idea Factory are Telegram bots that make it easier to use existing platforms. Both are very simple concepts that offer results that can improve your daily life. 

Creating Telegram Bots to Make Things Easier 

Think about the last time that you used Netflix. If you’re like most of us, you probably love the streaming platform but find it’s interface annoying. How do you find what you’re looking for? Sure, there are categories with movie genres but they are notoriously hard to navigate. For years, now it’s been a joke that people spend more time searching through the menu than they do actually viewing content. 

But, what if there were a friend to help? Enter Telegram bots to the rescue! One of our brilliant entrepreneurs at the Idea Factory came up with the idea of a Netflix bot. All you do is tell it what you want to watch and it will search the complete archives.

Imagine simply sending a message that says, “I want to find a new mystery series that I’ll enjoy.” The bot then might ask if you want to watch something based on a real-life story or if you want to check out a new thriller. The bot might ask you what language you prefer or if you are in the mood for Hollywood or Bollywood. Rather than endless scroll, you can simply choose A or B until the bot narrows it down to the perfect pick. Forget generic recommendations and endless lists. This is a way to see exactly what you are in the mood for right now. 

Creating Telegram to Increase Access

Another smart use of a Telegram bot is to help people find work. There are some great sites to find online work. Much of this world can be done directly on a mobile phone. But, how do you get started with this? There are so many online job sites and project platforms and, worst yet, the registration for each one can take ages. 

What if you could simply chat with a job search assistant who would connect you directly to the perfect opportunity? You could answer all of the key information that you might have included in your CV, this be matched with the online that would start earning you cash directly from your mobile. 

Business to Business Telegram Bots 

One of our favourite new startup concepts is the bot-based Inqoob. This cool new service allows startups to develop their own tools in minutes. From creating an advertising network to developing a production center for talent support, this is a do-it-all bot creator. They offer easy-to-create presentations and quizzes, and you can even create your own online story. 

When you arrive on the platform, you’ll see a range of bots that you can (or you can sell your own). CONTACT/WEBSITE to check it out. 


Step to Step Telegram Bots

Not quite sure how to code your own Telegram bot, but want to learn? Keep reading right here for your step by step guide to creating a Telegram bot. 

What you need to create a Telegram bot

To create your first bot, start with Python 3, python-telegram-bot, and the public API RandomDog.

Step One: Generate a token

Before you start to write the program, you’ll need to generate a token for your bot. When you go to access the Telegram API, this token will be essential so start with it.

Step Two: Create a new bot 

Before you make a bot in Telegram, you need to register a bot. When you do this registration, you’ll get a token to access the Telegram API.

An easy way to do this is to go to BotFather. Make sure your Telegram app is open if you’re on desktop, then click on the link. 

This will open up a new conversation. 


Simply type  /newbot then send the message. You will be asked to name your bot, then create a user name for it. Once you have followed these two steps, it will give you an API. 

You can now access your bot by going to your URL: https://telegram.me/YOUR_BOT_USERNAME (see example in the photo above). 

Step Three: Install the library 

In Windows, open the command prompt. On a Mac, open the terminal. Install the library using the following command: pip3 install python-telegram-bot.

Step Four: Important libraries  

Since we are using the random dog API, the first goal is to get the bot to return a picture of a dog. 

Use the public API from RandomDog to generate these pics. 

The workflow is: access the API -> get the image URL -> send the image. To do that, you’ll want to import all of the library. Here’s how: 

From telegram.ext import Updater, CommandHandler

import requests

import re

Step Five: Access the API and get the image URL

Now you’ll want to create a function to get the URL. By using the requests library, you can access the API and get the json data. Here’s how: 

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

You can check the json data by going to this following URL: 

https://random.dog/woof.json 

You should see something like the following. 


Take that image URL because you’ll need the parameter so you can send the image.

image_url = contents['URL']

Next up, you’ll need to wrap the code into a function called get_url() . Here’s how: 

def get_url():

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

    url = contents['url']

    return url

Step Five: Send the image

To get your pic sent, you’ll need two parameters: your image URL and the recipient’s ID. This can be a group ID or user ID.

You can get the image URL by calling that get_url() function.

url = get_url() 

Find your recipient’s ID using this code:

chat_id = update.message.chat_id

Once you’ve got the image URL and your 

recipient’s ID, it’s time to send the message, which is an image.

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

Wrap that code in a function called bop, and make sure your code looks like this:

def bop(bot, update):

    url = get_url()

    chat_id = update.message.chat_id

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

Step Six: Create the main function

Finally, you'll want to create a function called main. This will run your program. 

Be sure to change YOUR_TOKEN with the token that you already generated. 

def main():

    updater = Updater('YOUR_TOKEN')

    dp = updater.dispatcher

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

    updater.start_polling()

    updater.idle()

if __name__ == '__main__':

    main()

When you are all done, your code will look like this:

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 bop(bot, update):

    url = get_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()

Step seven: Run the program

You're all done! Now it's time to test it.  Save your file and name it main.py.

Run your program using the following command:

python3 main.py

Now go to the following URL (with your bot name added) to test your new telegraph bot:

https://telegram.me/YOUR_BOT_USERNAME.

Send the /bop command. 

If everything is working, the bot will show you a picture of a dog.

Exploring options

You can use this RandomDog API to send all kinds of images, videos, and GIFs. Of course there is much more that you'll want to do with your bot than just send pictures of dogs. But look at what you've already created! In a short time, with just a few steps, you created a Telegram bot. 

What will you do with your Telegram bot? 

Use #research and tell us how Telegram bots fit into your plan. 


Report Page