Let’s show another echobot from their README:

import telebot

bot = telebot.TeleBot("TOKEN")

def send_welcome(message):
    bot.reply_to(message, "Howdy, how are you doing?")

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


Now we rewrite it to use Telethon:

from telethon import TelegramClient, events

bot = TelegramClient('bot', 11111, 'a1b2c3d4').start(bot_token='TOKEN')

async def send_welcome(event):
    await event.reply('Howdy, how are you doing?')

async def echo_all(event):
    await event.reply(event.text)


Key differences:

  • Instead of doing bot.reply_to(message), we can do event.reply. Note that the event behaves just like their message.
  • Telethon also supports func=lambda m: True, but it’s not necessary.

0 条评论


Avatar placeholder