from telegram.ext import Updater updater = Updater(token='TOKEN', use_context=True) def start(update, context): context.bot.send_message(chat_id=update.message.chat_id, text='Hello!') updater.dispatcher.add_handler(CommandHandler('start', start)) updater.start_polling()
import telegram bot = telegram.Bot(token='TOKEN') bot.send_message(chat_id='USER_ID', text='Hello!')
from telegram.ext import Updater, MessageHandler, Filters def reply_to_message(update, context): message_text = update.message.text context.bot.send_message(chat_id=update.message.chat_id, text='You said: ' + message_text) updater.dispatcher.add_handler(MessageHandler(Filters.text & (~Filters.command), reply_to_message)) updater.start_polling()In this example, we define a reply_to_message() function that sends a reply to a user's message. We use the MessageHandler class to add a handler that triggers the reply_to_message() function when the bot receives a text message that is not a command. We start polling for updates as usual. Package library used: python-telegram-bot Overall, the package library used in all these examples is python-telegram-bot, which provides a user-friendly interface for building Telegram bots using Python. It handles low-level details of the Telegram Bot API, such as sending HTTP requests and parsing JSON responses, so developers can focus on bot logic and functionality.