Python Telegram Bot get the bot to respond to message
You can use the User
object of the inline query to send them a message. Keep in mind that the user has to have started a private chat with the bot before the bot can send them messages.
I modified your attempt. It should work, but i have not tested it:
def inlinequery(update, context):
"""Handle the inline query."""
query = update.inline_query
text = query.query
print(text)
query.from_user.send_message(text)
Related docs:
Related videos on Youtube
nyjets30
Updated on June 04, 2022Comments
-
nyjets30 almost 2 years
I am currently using
python-telegram-bot
library to make a telegram bot. My problem is I am trying to have my bot respond back when using the inline command. So when a user sends the bot@botname 'text'
, I want the to store the'text'
as astring
and then have my bot send something back with that variable.For some reason I can not get this to work. I tried the code below, but it doesn't work...I also posted the example from the github that works but not in the way i want.
My code
def inlinequery(update, context): """Handle the inline query.""" query = update.inline_query.query text = query.message_text print(text) update.message.reply_text(text)
Example Code
#Sends message when @botname is used def inlinequery(update, context): """Handle the inline query.""" query = update.inline_query.query results = [ InlineQueryResultArticle( id=uuid4(), title="Caps", input_message_content=InputTextMessageContent( query.upper())), InlineQueryResultArticle( id=uuid4(), title="Bold", input_message_content=InputTextMessageContent( "*{}*".format(escape_markdown(query)), parse_mode=ParseMode.MARKDOWN)), InlineQueryResultArticle( id=uuid4(), title="Italic", input_message_content=InputTextMessageContent( "_{}_".format(escape_markdown(query)), parse_mode=ParseMode.MARKDOWN))] update.inline_query.answer(results) def main(): # Get the dispatcher to register handlers dp = updater.dispatcher dp.add_handler(InlineQueryHandler(inlinequery)) # Start the Bot updater.start_polling() if __name__ == '__main__': main()