Basic Telegram Bot

Telegram Bot API

Регистрация бота

BotFather

BotFather

Способ получения сообщений

Формат запросов

Long Polling

Long Polling

Библиотеки для работы с TG API

requests!

requests!

import requests
 
base_url = 'https://api.telegram.org/bot123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11/'

while True:
    api_response = requests.get(base_url + 'getMe').json()
    
    for update in api_response:
        chat_id = update['chat_id']
        message = update['message']['text']
        reply_message = {
           "chat_id": chat_id,
           "text": message,
        }
        requests.post(base_url + 'sendMessage', json=reply_message)

python-telegram-bot

from telegram.ext import Updater, CommandHandler, MessageHandler, Filters

def echo(update, context):
    """Echo the user message."""
    update.message.reply_text(update.message.text)


updater = Updater("TOKEN", use_context=True)

dp.add_handler(MessageHandler(Filters.text, echo))

updater.start_polling()

   

pyTelegramBotAPI

import telebot

bot = telebot.TeleBot('<api_token>')

@bot.message_handler(func=lambda message: True)
def echo_message(message):
    bot.reply_to(message, message.text)

bot.polling()

Echo Bot

Погодный бот

Диалоговый

Без памяти

[54636] Telegram Bot

By b0g3r

[54636] Telegram Bot

  • 424