I try to create a Telegram bot but I can't use property Update class.
import logging
from multiprocessing import Queue
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import Updater, CommandHandler, CallbackQueryHandler
def main():
update_queue = Queue()
updater = Updater("API KEY", use_context=True, update_queue=update_queue)
updater.dispatcher.add_handler(CommandHandler('start', start))
updater.dispatcher.add_handler(CallbackQueryHandler(button))
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()
Idle say that there is no dispatcher in Update class. Try update Telegram api, didn't help. Can't find another way to update bot
Since version 20.0 the Updater is only used to fetch updates, from the docs:
Changed in version 20.0:
Removed argument and attribute user_sig_handler
The only arguments and attributes are now bot and update_queue as now the sole purpose of this class is to fetch updates. The entry point to a PTB application is now telegram.ext.Application.
So if you want to add a handler, use an Application.
An example, taken from the echo bot example:
async def echo(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Echo the user message."""
await update.message.reply_text(update.message.text)
def main() -> None:
"""Start the bot."""
# Create the Application and pass it your bot's token.
application = Application.builder().token("TOKEN").build()
# on different commands - answer in Telegram
application.add_handler(CommandHandler("start", start))
application.add_handler(CommandHandler("help", help_command))
# on non command i.e message - echo the message on Telegram
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, echo))
# Run the bot until the user presses Ctrl-C
application.run_polling()
if __name__ == "__main__":
main()
Related
I got error when running python script to integrate openai with telegram bot. here is my code:
# Import the necessary modules
import telegram
import openai
from telegram.ext import CommandHandler, MessageHandler, Updater
from queue import Queue
# Initialize the Telegram bot
bot = telegram.Bot(token='')
# Initialize the OpenAI API
openai.api_key = ''
# Define a function to handle /start command
def start(bot, update):
bot.send_message(chat_id=update.message.chat_id, text="Hi! I'm a ChatGPT bot. Send me a message and I'll try to respond to it.")
# Define a function to handle messages
def message(bot, update):
# Get the message text
message_text = update.message.text
# Call the OpenAI API to generate a response
response = openai.Completion.create(
engine="davinci",
prompt=message_text,
max_tokens=1024,
n=1,
stop=None,
temperature=0.5,
)
# Get the response text from the API
response_text = response.choices[0].text.strip()
# Send the response back to the user
bot.send_message(chat_id=update.message.chat_id, text=response_text)
# Initialize the Telegram bot updater and dispatcher
update_queue = Queue()
updater = Updater(bot=bot, update_queue=update_queue)
dispatcher = updater.dispatcher
# Add command handlers
start_handler = CommandHandler('start', start)
dispatcher.add_handler(start_handler)
# Add message handler
message_handler = MessageHandler(None, message)
dispatcher.add_handler(message_handler)
# Start the Telegram bot
updater.start_polling()
The error :
dispatcher = updater.dispatcher
^^^^^^^^^^^^^^^^^^
AttributeError: 'Updater' object has no attribute 'dispatcher'
I don't know how to fix this, because I've already see many solution but it tell me to update telegram-bot.
is there any solution to this? I've already upgrade telegram-bot but still error.
I think 'dispatcher' is no longer used in version 20.0a0
If you want to use your code then you can downgrade as:
pip install python-telegram-bot==13.3
If you are using v20 then you have to build your application differently and you have to use async functions.
I develop a telegram bot using the python-telegram-bot module. I attempt to run a function without executing it using dispatcher.run_async(myfunction) but how I can do a task for example sending a message from inside the dispatcher.run_async()?
I have all the users id in my database. And this is the snippet of my code.
import telegram
from telegram.ext import Updater
def myfunction():
# bot.send_message(text='Hello, World',chat_id=<user_id>)
def main():
"""Start the bot."""
# Create the Updater and pass it your bot's token.
updater = Updater("TOKEN")
# Get the dispatcher to register handlers
dispatcher = updater.dispatcher
dispatcher.run_async(myfunction)
# Start the Bot
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()
Not sure if this is the intended way, but you can pass the bot the the function by passing it to run_async:
dispatcher.run_async(myFunction, updater.bot)
def myfunction(bot):
bot.send_message(text='Hello, World',chat_id=123456789)
import telegram
from telegram.ext import Updater
def myfunction(bot):
bot.send_message(text='Hello, World',chat_id=123456789)
def main():
"""Start the bot."""
# Create the Updater and pass it your bot's token.
updater = Updater("<MY-BOT-TOKEN>")
# Get the dispatcher to register handlers
dispatcher = updater.dispatcher
dispatcher.run_async(myfunction, updater.bot)
# Start the Bot
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()
I am trying to deploy a simple echo2 bot using webhook. It's working fine with polling. But, I want to use the bot using webhook. I didn't find any easy to understand answer for this question on internet.
What changes do I need to make in the below code to make the bot work using webhook..?
import logging
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
# Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
logger = logging.getLogger(__name__)
# Define a few command handlers. These usually take the two arguments update and
# context. Error handlers also receive the raised TelegramError object in error.
def start(update, context):
"""Send a message when the command /start is issued."""
update.message.reply_text('Hi!')
def help(update, context):
"""Send a message when the command /help is issued."""
update.message.reply_text('Help!')
def echo(update, context):
"""Echo the user message."""
update.message.reply_text(update.message.text)
def error(update, context):
"""Log Errors caused by Updates."""
logger.warning('Update "%s" caused error "%s"', update, context.error)
def main():
"""Start the bot."""
# Create the Updater and pass it your bot's token.
# Make sure to set use_context=True to use the new context based callbacks
# Post version 12 this will no longer be necessary
updater = Updater("TOKEN", use_context=True)
# Get the dispatcher to register handlers
dp = updater.dispatcher
# on different commands - answer in Telegram
dp.add_handler(CommandHandler("start", start))
dp.add_handler(CommandHandler("help", help))
# on noncommand i.e message - echo the message on Telegram
dp.add_handler(MessageHandler(Filters.text, echo))
# log all errors
dp.add_error_handler(error)
# Start the Bot
updater.start_polling()
# Run the bot until you press Ctrl-C or the process receives SIGINT,
# SIGTERM or SIGABRT. This should be used most of the time, since
# start_polling() is non-blocking and will stop the bot gracefully.
updater.idle()
if __name__ == '__main__':
main()
You can just remove the updater.start_polling() call and use this instead.
Webhook for Heroku python-telegram-bot
Make sure you've already created your application on heroku.
Using the webhook:
# Use env variable PORT
PORT = int(os.environ.get("PORT", 3978))
updater.start_webhook(listen="0.0.0.0", port=PORT, url_path='TELEGRAM_TOKEN')
updater.bot.setWebhook('https://mywebhook/TELEGRAM_TOKEN')
Notes:
set your TELEGRAM_TOKEN received by the BotFather
set the relevant port (that might depend on the app running your webhook): the example above shows how to bind the port on Heroku
webhook must listen on HTTPS
I'm trying to make a telegram bot with https://github.com/python-telegram-bot/python-telegram-bot library in python3.
I want to make the bot send me infos from a dictionary that will be updated later on. This is my code now:
from secret import botToken
import logging
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
from multiprocessing import Process, Manager
def f(dicts):
dicts['runNumber'] = 1
def runUp(dicts):
dicts['runNumber'] += 1
############################# Telegram Bot part #################################
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
logger = logging.getLogger(__name__)
def botStart(update, context):
"""Send a message when the command /start is issued."""
update.message.reply_text("Hey ! do /help or /test ")
def test(update, context):
"""Send a message when the command /test is issued."""
runUp(d)
update.message.reply_text("run number is now : {0}".format(d['runNumber']))
def help(update, context):
"""Send a message when the command /help is issued."""
update.message.reply_text("HELP !!")
def echo(update, context):
"""Send a message when message without command is issued."""
update.message.reply_text("echo")
def error(update, context):
"""Log Errors caused by Updates."""
logger.warning('Update "%s" caused error "%s"', update, context.error)
def main():
"""Start the bot."""
updater = Updater(botToken, use_context=True)
# Get the dispatcher to register handlers
dp = updater.dispatcher
# on different commands - answer in Telegram
dp.add_handler(CommandHandler("start", botStart))
dp.add_handler(CommandHandler("help", help))
dp.add_handler(CommandHandler("test", test))
# on noncommand i.e message - answer in telegram
dp.add_handler(MessageHandler(Filters.text, echo))
# log all errors
dp.add_error_handler(error)
# Start the Bot
updater.start_polling()
# Run the bot until you press Ctrl-C or the process receives SIGINT,
# SIGTERM or SIGABRT. This should be used most of the time, since
# start_polling() is non-blocking and will stop the bot gracefully.
updater.idle()
#################################################################################
if __name__ == '__main__':
manager = Manager()
d = manager.dict()
p1 = Process(target=f, args=(d,))
p1.start()
p1.join()
p2 = Process(target=main)
p2.start()
but everytime I run the /test command i get this feedback from the logging in the python window:
... caused error "name 'd' is not defined"
Note : I can't do without the multiprocessing since it is used somewhere else in my code
In the function test(update, context), d is really not defined (it is present in code but not visible from the test method). You need to explicitly use d as a global variable or add it as a parameter of the function test (and it callers).
i am developing a python script for my telegram right now. The problem is:
How do I know when my bot is added to a group? Is there an Event or something else for that?
I want the Bot to send a message to the group he´s beeing added to which says hi and the functions he can.
I dont know if any kind of handler is able deal with this.
Very roughly, you would need to do something like this: register an handler that filters only service messages about new chat members. Then check if the bot is one of the new chat members.
from telegram.ext import Updater, MessageHandler, Filters
def new_member(bot, update):
for member in update.message.new_chat_members:
if member.username == 'YourBot':
update.message.reply_text('Welcome')
updater = Updater('TOKEN')
updater.dispatcher.add_handler(MessageHandler(Filters.status_update.new_chat_members, new_member))
updater.start_polling()
updater.idle()
With callbacks (preferred)
As of version 12, the preferred way to handle updates is via callbacks. To use them prior to version 13 state use_context=True in your Updater. Version 13 will have this as default.
from telegram.ext import Updater, MessageHandler, Filters
def new_member(update, context):
for member in update.message.new_chat_members:
if member.username == 'YourBot':
update.message.reply_text('Welcome')
updater = Updater('TOKEN', use_context=True) # use_context will be True by default in version 13+
updater.dispatcher.add_handler(MessageHandler(Filters.status_update.new_chat_members, new_member))
updater.start_polling()
updater.idle()
Please note that the order changed here. Instead of having the update as second, it is now the first argument. Executing the code below will result in an Exception like this:
AttributeError: 'CallbackContext' object has no attribute 'message'
Without callbacks (deprecated in version 12)
Blatantly copying from mcont's answer:
from telegram.ext import Updater, MessageHandler, Filters
def new_member(bot, update):
for member in update.message.new_chat_members:
if member.username == 'YourBot':
update.message.reply_text('Welcome')
updater = Updater('TOKEN')
updater.dispatcher.add_handler(MessageHandler(Filters.status_update.new_chat_members, new_member))
updater.start_polling()
updater.idle()