Showing posts with label Programming. Show all posts
Showing posts with label Programming. Show all posts
In Part-1, We learn from very beginning that how to create a bot using Telegram and python-telegram-bot,

In this tutorial, we will code a simple bot that will reply with the same message that you will send to the bot. we can simply name it as a "auto-reply" bot.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/usr/bin/env python
import telegram
from telegram.error import NetworkError, Unauthorized
from time import sleep

update_id = None

def main():
    global update_id
    bot = telegram.Bot('TOKEN')
    echo(bot)

def echo(bot):

    global update_id
    for update in bot.get_updates(offset=update_id, timeout=10):
        update_id = update.update_id + 1

        if update.message:  # your bot can receive updates without messages
            # Reply to the message
            update.message.reply_text(update.message.text)

if __name__ == '__main__':
main()  

Some Other Techniques for Making Interesting Bot
  • Like you can use any API that is taking some parameters and returning you the specific response, get the response make as you want and you can use it with a telegram bot. 



  • What i have done is that i have explored an API that takes mathematical sums and returns the answer, i create a simple telegram bot named as MathsMagic Bot, after then i take input from the user through that bot, passes the input to that API and show the response to the user. Same approach can be applied to Cryptocurrency rates and other useful things.
  • Another idea was to create an Image Analytics Bot, that takes an image from the user, pass that image to API or tool that will perform analysis on it and gives back the result, Post the result as output to that bot.
This is how you can use Telegram bot with multiple APIs and tools and create your own interesting Bot. Go for it and wait for the third tutorial in which we will explore the Telegram Bot further and see its other features also like creating a menu inside your bot and creating a group based Bot and manymore.

AUTOMATED BOTS

Bots are really interesting and easy to make, you just have to learn how it is being made just simply using multiple concepts, here in this article i will tell you a little about how to make a bot using an open source messaging application "Telegram".
Follow just a couple of simple steps and learn how to use python to create an interesting automated bots for usage.
You need couple of things to create a bot;
1) Telegram Application.
2) A little Knowledge of Python Programming.

You can write a Telegram chat bot in any language you want. Some of the main options apart from Python would be Java, PHP, or Ruby. If you are more familiar with a different high-level programming language, then you might prefer to use that instead, but here i am using Python, because it is easy to use and learn; As a matter of first importance, to make bot you need to have a Telegram account. Follow these steps;

  • Add BotFather to your contact list.


  • Begin a discussion with BotFather by clicking Start catch. You will see an order list quickly.

  • Keeping in mind the end goal to make the bot, you need to type /newbot charge and take after the directions. Be watchful, your bot's username needs to end with bot. For instance, MathsBot or CryptoBot

  • After selecting the username, you can access the token of your bot, from botfather, 
    • Select mybots from the options.
    • Select your own bot, and after that click API TOKEN, and a long string will be generated that will be the token of your bot.
  • We're finished with all the Telegram setup for our bot. Next introduce python-telegram-bot library:

    pip install python-telegram-bot

    python-telegram-bot library gives an awesome python interface to Telegram Bot API. It has a few reflections which makes the advancement of bots simple and clear. This library will deal with sending message to our API URL and get the reply or response from that URL. Here is the complete code for python-telegram-bot Github 
  •  Below is the code to send Hello along with your username message to your Bot.

from telegram.ext import Updater, CommandHandler
def hello(bot, update):
    update.message.reply_text(
        'Hello {}'.format(update.message.from_user.first_name))

updater = Updater('YOUR TOKEN HERE')

updater.dispatcher.add_handler(CommandHandler('hello', hello))
updater.start_polling()
updater.idle()

  •  More Understanding about the code and making the Bot interactive will be in the next part of this series.
So, Let's create a bot and make the things automated.