2016-07-15 01:30:54 +02:00
|
|
|
#!/usr/bin/env python
|
2023-09-09 23:12:30 +02:00
|
|
|
# pylint: disable=unused-argument
|
2016-07-15 01:30:54 +02:00
|
|
|
# This program is dedicated to the public domain under the CC0 license.
|
|
|
|
|
2019-02-13 13:38:07 +01:00
|
|
|
"""
|
2016-07-15 01:30:54 +02:00
|
|
|
First, a few callback functions are defined. Then, those functions are passed to
|
2022-04-24 12:38:09 +02:00
|
|
|
the Application and registered at their respective places.
|
2016-07-15 01:30:54 +02:00
|
|
|
Then, the bot is started and runs until we press Ctrl-C on the command line.
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
Example of a bot-user conversation using ConversationHandler.
|
|
|
|
Send /start to initiate the conversation.
|
|
|
|
Press Ctrl-C on the command line or send a signal to the process to stop the
|
|
|
|
bot.
|
|
|
|
"""
|
|
|
|
|
2018-09-21 08:57:01 +02:00
|
|
|
import logging
|
|
|
|
|
2020-10-31 16:33:34 +01:00
|
|
|
from telegram import ReplyKeyboardMarkup, ReplyKeyboardRemove, Update
|
|
|
|
from telegram.ext import (
|
2022-05-05 09:27:54 +02:00
|
|
|
Application,
|
2020-10-31 16:33:34 +01:00
|
|
|
CommandHandler,
|
2022-05-12 19:36:25 +02:00
|
|
|
ContextTypes,
|
2022-05-05 09:27:54 +02:00
|
|
|
ConversationHandler,
|
2020-10-31 16:33:34 +01:00
|
|
|
MessageHandler,
|
2021-11-20 11:36:18 +01:00
|
|
|
filters,
|
2020-10-31 16:33:34 +01:00
|
|
|
)
|
2016-07-15 01:30:54 +02:00
|
|
|
|
|
|
|
# Enable logging
|
2020-10-09 17:22:07 +02:00
|
|
|
logging.basicConfig(
|
2022-05-05 17:40:22 +02:00
|
|
|
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
|
2020-10-09 17:22:07 +02:00
|
|
|
)
|
2023-06-07 22:32:04 +02:00
|
|
|
# set higher logging level for httpx to avoid all GET and POST requests being logged
|
|
|
|
logging.getLogger("httpx").setLevel(logging.WARNING)
|
|
|
|
|
2016-07-15 01:30:54 +02:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
GENDER, PHOTO, LOCATION, BIO = range(4)
|
|
|
|
|
|
|
|
|
2022-05-12 19:36:25 +02:00
|
|
|
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
|
2021-05-27 20:34:58 +02:00
|
|
|
"""Starts the conversation and asks the user about their gender."""
|
2022-05-05 17:40:22 +02:00
|
|
|
reply_keyboard = [["Boy", "Girl", "Other"]]
|
2016-07-15 01:30:54 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
await update.message.reply_text(
|
2022-05-05 17:40:22 +02:00
|
|
|
"Hi! My name is Professor Bot. I will hold a conversation with you. "
|
|
|
|
"Send /cancel to stop talking to me.\n\n"
|
|
|
|
"Are you a boy or a girl?",
|
2021-07-01 17:45:19 +02:00
|
|
|
reply_markup=ReplyKeyboardMarkup(
|
2022-05-05 17:40:22 +02:00
|
|
|
reply_keyboard, one_time_keyboard=True, input_field_placeholder="Boy or Girl?"
|
2021-07-01 17:45:19 +02:00
|
|
|
),
|
2020-10-09 17:22:07 +02:00
|
|
|
)
|
2016-07-15 01:30:54 +02:00
|
|
|
|
|
|
|
return GENDER
|
|
|
|
|
|
|
|
|
2022-05-12 19:36:25 +02:00
|
|
|
async def gender(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
|
2021-05-27 20:34:58 +02:00
|
|
|
"""Stores the selected gender and asks for a photo."""
|
2016-07-15 01:30:54 +02:00
|
|
|
user = update.message.from_user
|
2017-10-20 20:24:00 +02:00
|
|
|
logger.info("Gender of %s: %s", user.first_name, update.message.text)
|
2022-04-24 12:38:09 +02:00
|
|
|
await update.message.reply_text(
|
2022-05-05 17:40:22 +02:00
|
|
|
"I see! Please send me a photo of yourself, "
|
|
|
|
"so I know what you look like, or send /skip if you don't want to.",
|
2020-10-09 17:22:07 +02:00
|
|
|
reply_markup=ReplyKeyboardRemove(),
|
|
|
|
)
|
2016-07-15 01:30:54 +02:00
|
|
|
|
|
|
|
return PHOTO
|
|
|
|
|
|
|
|
|
2022-05-12 19:36:25 +02:00
|
|
|
async def photo(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
|
2021-05-27 20:34:58 +02:00
|
|
|
"""Stores the photo and asks for a location."""
|
2016-07-15 01:30:54 +02:00
|
|
|
user = update.message.from_user
|
2022-04-24 12:38:09 +02:00
|
|
|
photo_file = await update.message.photo[-1].get_file()
|
2022-11-24 12:09:51 +01:00
|
|
|
await photo_file.download_to_drive("user_photo.jpg")
|
2022-05-05 17:40:22 +02:00
|
|
|
logger.info("Photo of %s: %s", user.first_name, "user_photo.jpg")
|
2022-04-24 12:38:09 +02:00
|
|
|
await update.message.reply_text(
|
2022-05-05 17:40:22 +02:00
|
|
|
"Gorgeous! Now, send me your location please, or send /skip if you don't want to."
|
2020-10-09 17:22:07 +02:00
|
|
|
)
|
2016-07-15 01:30:54 +02:00
|
|
|
|
|
|
|
return LOCATION
|
|
|
|
|
|
|
|
|
2022-05-12 19:36:25 +02:00
|
|
|
async def skip_photo(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
|
2021-05-27 20:34:58 +02:00
|
|
|
"""Skips the photo and asks for a location."""
|
2016-07-15 01:30:54 +02:00
|
|
|
user = update.message.from_user
|
2017-10-20 20:24:00 +02:00
|
|
|
logger.info("User %s did not send a photo.", user.first_name)
|
2022-04-24 12:38:09 +02:00
|
|
|
await update.message.reply_text(
|
2022-05-05 17:40:22 +02:00
|
|
|
"I bet you look great! Now, send me your location please, or send /skip."
|
2020-10-09 17:22:07 +02:00
|
|
|
)
|
2016-07-15 01:30:54 +02:00
|
|
|
|
|
|
|
return LOCATION
|
|
|
|
|
|
|
|
|
2022-05-12 19:36:25 +02:00
|
|
|
async def location(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
|
2021-05-27 20:34:58 +02:00
|
|
|
"""Stores the location and asks for some info about the user."""
|
2016-07-15 01:30:54 +02:00
|
|
|
user = update.message.from_user
|
|
|
|
user_location = update.message.location
|
2020-10-09 17:22:07 +02:00
|
|
|
logger.info(
|
|
|
|
"Location of %s: %f / %f", user.first_name, user_location.latitude, user_location.longitude
|
|
|
|
)
|
2022-04-24 12:38:09 +02:00
|
|
|
await update.message.reply_text(
|
2022-05-05 17:40:22 +02:00
|
|
|
"Maybe I can visit you sometime! At last, tell me something about yourself."
|
2020-10-09 17:22:07 +02:00
|
|
|
)
|
2016-07-15 01:30:54 +02:00
|
|
|
|
|
|
|
return BIO
|
|
|
|
|
|
|
|
|
2022-05-12 19:36:25 +02:00
|
|
|
async def skip_location(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
|
2021-05-27 20:34:58 +02:00
|
|
|
"""Skips the location and asks for info about the user."""
|
2016-07-15 01:30:54 +02:00
|
|
|
user = update.message.from_user
|
2017-10-20 20:24:00 +02:00
|
|
|
logger.info("User %s did not send a location.", user.first_name)
|
2022-04-24 12:38:09 +02:00
|
|
|
await update.message.reply_text(
|
2022-05-05 17:40:22 +02:00
|
|
|
"You seem a bit paranoid! At last, tell me something about yourself."
|
2020-10-09 17:22:07 +02:00
|
|
|
)
|
2016-07-15 01:30:54 +02:00
|
|
|
|
|
|
|
return BIO
|
|
|
|
|
|
|
|
|
2022-05-12 19:36:25 +02:00
|
|
|
async def bio(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
|
2021-05-27 20:34:58 +02:00
|
|
|
"""Stores the info about the user and ends the conversation."""
|
2016-07-15 01:30:54 +02:00
|
|
|
user = update.message.from_user
|
2017-10-20 20:24:00 +02:00
|
|
|
logger.info("Bio of %s: %s", user.first_name, update.message.text)
|
2022-05-05 17:40:22 +02:00
|
|
|
await update.message.reply_text("Thank you! I hope we can talk again some day.")
|
2016-07-15 01:30:54 +02:00
|
|
|
|
|
|
|
return ConversationHandler.END
|
|
|
|
|
|
|
|
|
2022-05-12 19:36:25 +02:00
|
|
|
async def cancel(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
|
2021-05-27 20:34:58 +02:00
|
|
|
"""Cancels and ends the conversation."""
|
2016-07-15 01:30:54 +02:00
|
|
|
user = update.message.from_user
|
2017-10-20 20:24:00 +02:00
|
|
|
logger.info("User %s canceled the conversation.", user.first_name)
|
2022-04-24 12:38:09 +02:00
|
|
|
await update.message.reply_text(
|
2022-05-05 17:40:22 +02:00
|
|
|
"Bye! I hope we can talk again some day.", reply_markup=ReplyKeyboardRemove()
|
2020-10-09 17:22:07 +02:00
|
|
|
)
|
2016-07-15 01:30:54 +02:00
|
|
|
|
|
|
|
return ConversationHandler.END
|
|
|
|
|
|
|
|
|
2020-10-31 16:33:34 +01:00
|
|
|
def main() -> None:
|
2021-05-27 20:34:58 +02:00
|
|
|
"""Run the bot."""
|
2022-04-24 12:38:09 +02:00
|
|
|
# Create the Application and pass it your bot's token.
|
|
|
|
application = Application.builder().token("TOKEN").build()
|
2016-07-15 01:30:54 +02:00
|
|
|
|
|
|
|
# Add conversation handler with the states GENDER, PHOTO, LOCATION and BIO
|
|
|
|
conv_handler = ConversationHandler(
|
2022-05-05 17:40:22 +02:00
|
|
|
entry_points=[CommandHandler("start", start)],
|
2016-07-15 01:30:54 +02:00
|
|
|
states={
|
2022-05-05 17:40:22 +02:00
|
|
|
GENDER: [MessageHandler(filters.Regex("^(Boy|Girl|Other)$"), gender)],
|
|
|
|
PHOTO: [MessageHandler(filters.PHOTO, photo), CommandHandler("skip", skip_photo)],
|
2020-10-09 17:22:07 +02:00
|
|
|
LOCATION: [
|
2021-11-20 11:36:18 +01:00
|
|
|
MessageHandler(filters.LOCATION, location),
|
2022-05-05 17:40:22 +02:00
|
|
|
CommandHandler("skip", skip_location),
|
2020-10-09 17:22:07 +02:00
|
|
|
],
|
2021-11-20 11:36:18 +01:00
|
|
|
BIO: [MessageHandler(filters.TEXT & ~filters.COMMAND, bio)],
|
2016-07-15 01:30:54 +02:00
|
|
|
},
|
2022-05-05 17:40:22 +02:00
|
|
|
fallbacks=[CommandHandler("cancel", cancel)],
|
2016-07-15 01:30:54 +02:00
|
|
|
)
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
application.add_handler(conv_handler)
|
2016-07-15 01:30:54 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
# Run the bot until the user presses Ctrl-C
|
2023-06-04 17:11:58 +02:00
|
|
|
application.run_polling(allowed_updates=Update.ALL_TYPES)
|
2016-07-15 01:30:54 +02:00
|
|
|
|
|
|
|
|
2022-05-05 17:40:22 +02:00
|
|
|
if __name__ == "__main__":
|
2016-07-15 01:30:54 +02:00
|
|
|
main()
|