Python 2.7 Python (2.7) Telegram Bot

Discussion in 'Coding' started by Sascha, May 30, 2017.

  1. Sascha

    Sascha Active Member Staff Member

    As many of you know already, telegram offers a open API for coding bots. This is an awesome replacement for E-Mail and SMS notifications. Here is a super simple example how to start making your own Python Telegram Bot.

    1. PIP & Library

    https://pip.pypa.io/en/stable/installing/

    After installing PIP , use the following command to install the required libary:
    Code:
    pip install pyTelegramBotAPI
    2. Create the bot and receive the tokens from Telegram

    https://core.telegram.org/bots#3-how-do-i-create-a-bot

    3. How to tell the bot in which channel to send messages to?


    Invite the bot to the channel or group where you want to send messages. Wait until a message is sent into the group or send a message yourself. Then visit: https://api.telegram.org/botTOKEN_HERE/getUpdates?offset=0 (replace the "TOKEN_HERE" with your bot token. It should look like this: "294044544:AAG-F6JIC7ptNTqadg345TI1NiQmHYMybOU") to get the chat id. It will look similar to this: "297178735".

    4. Source code:

    Code:
    import telebot
    
    tb = telebot.TeleBot(token)
    tb.send_message(chatid, 'hey. i am the bot!')
    Long and Detailed Guide:
    https://pypi.python.org/pypi/pyTelegramBotAPI/3.0.0

    Edit: Thanks to Telegram User "aknownstranger" for pointing out a pip typo.
     
    Last edited: May 30, 2017
    JohnDoe likes this.