basic telegram bot example - python

I am recreating the basic telegram bot example from here, but I am having a slight problem.
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 bot and
# update. 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("YOUR TOKEN HERE",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()
I am getting below error after executing the script:
$ python test1.py
Traceback (most recent call last):
File "test1.py", line 64, in <module>
main()
File "test1.py", line 39, in main
updater = Updater(TOKEN,use_context=True)
TypeError: __init__() got an unexpected keyword argument 'use_context'

use_context is available as of version 12 of python-telegram-bot. You can find out the version you have installed via pip show python-telegram-bot.
The simplest solution would be to just remove the parameter use_context, i.e. replace
updater = Updater("YOUR TOKEN HERE",use_context=True)
with
updater = Updater("YOUR TOKEN HERE")

The error is telling you that use_context is not a valid keyword argument for the Updater initializer. That argument is no longer supported in the 12th version of Python Telegram Bot.
I guess you did pip install python-telegram-bot==12.0.0b1 --upgrade to install the library and it's installing the version 12.0.0b1.
You can do again the installation by running the following commnads:
Uninstall the current version of python-telegram-bot: pip uninstall python-telegram-bot
Install the 11 version of the library: pip install python-telegram-bot==11
Also, if you don't want to change your library version and keep using python-telegram-bot 12.0.0, you can just remove the use_context argument from the instantiation of the Updater class.
This line:
updater = Updater("YOUR TOKEN HERE",use_context=True)
Would become:
updater = Updater("YOUR TOKEN HERE")

Related

I got error AttributeError: 'Updater' object has no attribute 'dispatcher'

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.

Unresolved attribute reference 'dispatcher' for class 'Updater'

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()

How to stop Telegram bot executing old code

I made my first simple telegram bot with python-telegram-bot. Here's the code:
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
token = '1234567890'
updater = Updater(token=token, use_context=True)
dispatcher = updater.dispatcher
def start(update, context):
context.bot.send_message(chat_id = update.effective_chat.id, text=f"hey,{update.effective_chat.username}!")
def unknown(update, context):
context.bot.send_message(chat_id=update.effective_chat.id, text="Sorry, didn't understand")
unknown_handler = MessageHandler(Filters.command, unknown)
start_handler = CommandHandler('start', start)
dispatcher.add_handler(start_handler)
dispatcher.add_handler(unknown_handler)
updater.start_polling()
I kept changing the message text in the code and then executing it, checking what happens in the bot. What I ended up is every time I do start, the bot sends any of the old message versions together with new one. I revoked the token and it helped.
My question is what happens under the hood and why the latest version of the code doesn't replace the old ones? Also, how can I deal with it without having to revoke token every time?

How to create webhook for telegram bot with heroku?

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

Getting variables updates inside a function (Telegram bot, Python3, python-telegram-bot library, multiprocessing)

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).

Categories