Movatterモバイル変換


[0]ホーム

URL:


ContentsMenuExpandLight modeDark modeAuto light/dark, in light modeAuto light/dark, in dark modeSkip to content
python-telegram-bot
v22.1
Logo
python-telegram-bot
v22.1

Reference

Resources

Project

Back to top

conversationbot.py

  1#!/usr/bin/env python  2# pylint: disable=unused-argument  3# This program is dedicated to the public domain under the CC0 license.  4  5"""  6First, a few callback functions are defined. Then, those functions are passed to  7the Application and registered at their respective places.  8Then, the bot is started and runs until we press Ctrl-C on the command line.  9 10Usage: 11Example of a bot-user conversation using ConversationHandler. 12Send /start to initiate the conversation. 13Press Ctrl-C on the command line or send a signal to the process to stop the 14bot. 15""" 16 17importlogging 18 19fromtelegramimportReplyKeyboardMarkup,ReplyKeyboardRemove,Update 20fromtelegram.extimport( 21Application, 22CommandHandler, 23ContextTypes, 24ConversationHandler, 25MessageHandler, 26filters, 27) 28 29# Enable logging 30logging.basicConfig( 31format="%(asctime)s -%(name)s -%(levelname)s -%(message)s",level=logging.INFO 32) 33# set higher logging level for httpx to avoid all GET and POST requests being logged 34logging.getLogger("httpx").setLevel(logging.WARNING) 35 36logger=logging.getLogger(__name__) 37 38GENDER,PHOTO,LOCATION,BIO=range(4) 39 40 41asyncdefstart(update:Update,context:ContextTypes.DEFAULT_TYPE)->int: 42"""Starts the conversation and asks the user about their gender.""" 43reply_keyboard=[["Boy","Girl","Other"]] 44 45awaitupdate.message.reply_text( 46"Hi! My name is Professor Bot. I will hold a conversation with you. " 47"Send /cancel to stop talking to me.\n\n" 48"Are you a boy or a girl?", 49reply_markup=ReplyKeyboardMarkup( 50reply_keyboard,one_time_keyboard=True,input_field_placeholder="Boy or Girl?" 51), 52) 53 54returnGENDER 55 56 57asyncdefgender(update:Update,context:ContextTypes.DEFAULT_TYPE)->int: 58"""Stores the selected gender and asks for a photo.""" 59user=update.message.from_user 60logger.info("Gender of%s:%s",user.first_name,update.message.text) 61awaitupdate.message.reply_text( 62"I see! Please send me a photo of yourself, " 63"so I know what you look like, or send /skip if you don't want to.", 64reply_markup=ReplyKeyboardRemove(), 65) 66 67returnPHOTO 68 69 70asyncdefphoto(update:Update,context:ContextTypes.DEFAULT_TYPE)->int: 71"""Stores the photo and asks for a location.""" 72user=update.message.from_user 73photo_file=awaitupdate.message.photo[-1].get_file() 74awaitphoto_file.download_to_drive("user_photo.jpg") 75logger.info("Photo of%s:%s",user.first_name,"user_photo.jpg") 76awaitupdate.message.reply_text( 77"Gorgeous! Now, send me your location please, or send /skip if you don't want to." 78) 79 80returnLOCATION 81 82 83asyncdefskip_photo(update:Update,context:ContextTypes.DEFAULT_TYPE)->int: 84"""Skips the photo and asks for a location.""" 85user=update.message.from_user 86logger.info("User%s did not send a photo.",user.first_name) 87awaitupdate.message.reply_text( 88"I bet you look great! Now, send me your location please, or send /skip." 89) 90 91returnLOCATION 92 93 94asyncdeflocation(update:Update,context:ContextTypes.DEFAULT_TYPE)->int: 95"""Stores the location and asks for some info about the user.""" 96user=update.message.from_user 97user_location=update.message.location 98logger.info( 99"Location of%s:%f /%f",user.first_name,user_location.latitude,user_location.longitude100)101awaitupdate.message.reply_text(102"Maybe I can visit you sometime! At last, tell me something about yourself."103)104105returnBIO106107108asyncdefskip_location(update:Update,context:ContextTypes.DEFAULT_TYPE)->int:109"""Skips the location and asks for info about the user."""110user=update.message.from_user111logger.info("User%s did not send a location.",user.first_name)112awaitupdate.message.reply_text(113"You seem a bit paranoid! At last, tell me something about yourself."114)115116returnBIO117118119asyncdefbio(update:Update,context:ContextTypes.DEFAULT_TYPE)->int:120"""Stores the info about the user and ends the conversation."""121user=update.message.from_user122logger.info("Bio of%s:%s",user.first_name,update.message.text)123awaitupdate.message.reply_text("Thank you! I hope we can talk again some day.")124125returnConversationHandler.END126127128asyncdefcancel(update:Update,context:ContextTypes.DEFAULT_TYPE)->int:129"""Cancels and ends the conversation."""130user=update.message.from_user131logger.info("User%s canceled the conversation.",user.first_name)132awaitupdate.message.reply_text(133"Bye! I hope we can talk again some day.",reply_markup=ReplyKeyboardRemove()134)135136returnConversationHandler.END137138139defmain()->None:140"""Run the bot."""141# Create the Application and pass it your bot's token.142application=Application.builder().token("TOKEN").build()143144# Add conversation handler with the states GENDER, PHOTO, LOCATION and BIO145conv_handler=ConversationHandler(146entry_points=[CommandHandler("start",start)],147states={148GENDER:[MessageHandler(filters.Regex("^(Boy|Girl|Other)$"),gender)],149PHOTO:[MessageHandler(filters.PHOTO,photo),CommandHandler("skip",skip_photo)],150LOCATION:[151MessageHandler(filters.LOCATION,location),152CommandHandler("skip",skip_location),153],154BIO:[MessageHandler(filters.TEXT&~filters.COMMAND,bio)],155},156fallbacks=[CommandHandler("cancel",cancel)],157)158159application.add_handler(conv_handler)160161# Run the bot until the user presses Ctrl-C162application.run_polling(allowed_updates=Update.ALL_TYPES)163164165if__name__=="__main__":166main()

State Diagram

        flowchart TB    %% Documentation: https://mermaid-js.github.io/mermaid/#/flowchart    A(("/start")):::entryPoint -->|Hi! My name is Professor Bot...| B((GENDER)):::state    B --> |"- Boy <br /> - Girl <br /> - Other"|C("(choice)"):::userInput     C --> |I see! Please send me a photo...| D((PHOTO)):::state    D --> E("/skip"):::userInput    D --> F("(photo)"):::userInput    E --> |I bet you look great!| G[\ /]:::userInput    F --> |Gorgeous!| G[\ /]    G --> |"Now, send me your location .."| H((LOCATION)):::state    H --> I("/skip"):::userInput    H --> J("(location)"):::userInput    I --> |You seem a bit paranoid!| K[\" "/]:::userInput    J --> |Maybe I can visit...| K    K --> |"Tell me about yourself..."| L(("BIO")):::state    L --> M("(text)"):::userInput    M --> |"Thanks and bye!"| End(("END")):::termination    classDef userInput  fill:#2a5279, color:#ffffff, stroke:#ffffff    classDef state fill:#222222, color:#ffffff, stroke:#ffffff    classDef entryPoint fill:#009c11, stroke:#42FF57, color:#ffffff    classDef termination fill:#bb0007, stroke:#E60109, color:#ffffff
On this page

[8]ページ先頭

©2009-2025 Movatter.jp