简体   繁体   中英

Python Telegram Bot get the bot to respond to message

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

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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM