telebot (KyleJamesWalker) Telegram Bot Library
This `telebot` library (version 0.0.5) by KyleJamesWalker is a minimalist Telegram bot library featuring simple route decorators for message handling. It was last updated in 2018 and appears to be abandoned, meaning it might not be compatible with the latest Telegram API changes and could have unaddressed issues. Crucially, it shares a package name with the significantly more popular and actively maintained `pyTelegramBotAPI` library, leading to frequent user confusion.
Common errors
-
ModuleNotFoundError: No module named 'telebot.TeleBot'
cause You installed `KyleJamesWalker/telebot` but are trying to use API calls (like `TeleBot` class) from the `pyTelegramBotAPI` library.fixIf you intended to use `pyTelegramBotAPI`, run `pip uninstall telebot` and then `pip install pyTelegramBotAPI`. If you intended to use `KyleJamesWalker/telebot`, use `from telebot import bot` and `my_bot = bot(TOKEN)` instead. -
AttributeError: 'TeleBot' object has no attribute 'handle_message'
cause You installed `pyTelegramBotAPI` but are trying to use the decorator-based routing (`@handle_message`) specific to the `KyleJamesWalker/telebot` library.fixIf you intended to use `KyleJamesWalker/telebot`, run `pip uninstall pyTelegramBotAPI` and then `pip install telebot`. If you intended to use `pyTelegramBotAPI`, use its `bot.message_handler(commands=['start'])` decorator or similar methods instead of `@handle_message`. -
TypeError: 'module' object is not callable
cause You have installed `KyleJamesWalker/telebot`, and its `bot` is a module, not a callable class named `TeleBot`.fixIf you intended to use `KyleJamesWalker/telebot`, initialize it with `from telebot import bot; my_bot = bot(TOKEN)`. If you intended to use `pyTelegramBotAPI`, ensure it's installed (`pip install pyTelegramBotAPI`) and use `from telebot import TeleBot; my_bot = TeleBot(TOKEN)`.
Warnings
- breaking Critical Name Collision with `pyTelegramBotAPI`
- deprecated Library is Abandoned and Potentially Outdated
- gotcha Basic Error Handling and Features
Install
-
pip install telebot
Imports
- bot
from telebot import TeleBot
from telebot import bot
- handle_message
from telebot.ext import MessageHandler
from telebot import handle_message
Quickstart
import os
from telebot import bot, handle_message
# This token should be obtained from BotFather on Telegram.
# For local testing, you can hardcode it, but for deployment use environment variables.
BOT_TOKEN = os.environ.get('TELEBOT_TOKEN', 'YOUR_BOT_TOKEN_HERE')
# Initialize the bot
my_bot = bot(BOT_TOKEN)
@handle_message("/start")
def start_command(message):
my_bot.post_message(message["chat"]["id"], "Hello! I am your simple bot.")
@handle_message("/hello")
def hello_command(message):
my_bot.post_message(message["chat"]["id"], "Hi there!")
@handle_message("default") # This acts as a fallback for any other message
def default_message(message):
my_bot.post_message(message["chat"]["id"], "I received: " + message["text"])
if __name__ == "__main__":
if BOT_TOKEN == 'YOUR_BOT_TOKEN_HERE':
print("WARNING: Please replace 'YOUR_BOT_TOKEN_HERE' with your actual bot token or set the TELEBOT_TOKEN environment variable.")
print("Starting KyleJamesWalker/telebot (version 0.0.5). Press Ctrl+C to stop.")
print("Using token ending in:", BOT_TOKEN[-4:])
# The run() method starts an infinite loop for polling
my_bot.run()