python 日历处理程序问题

问题描述 投票:0回答:0

我正在尝试将日期选择器添加到以下脚本

import telegram
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import Updater, CommandHandler, ConversationHandler, CallbackQueryHandler, MessageHandler, Filters
import telegramcalendar
# Define the different states of the conversation
DEPARTURE, RETURN, TRIP_TYPE, CLASS, PASSENGERS = range(5)


# def calendar_handler(update, context):
#     query = update.callback_query

#     # Create the calendar
#     reply_markup = telegramcalendar.create_calendar()
    
#     # Send the calendar to the user
#     query.edit_message_text(text="Please select a date:",
#                             reply_markup=reply_markup)
#     query.answer()
    

# Define the start function
def start(update, context):
    keyboard = [
        [InlineKeyboardButton("One-way", callback_data='one-way'),
         InlineKeyboardButton("Round trip", callback_data='round trip')]
    ]
    reply_markup = InlineKeyboardMarkup(keyboard)
    update.message.reply_text('Welcome to the travel bot! What would you like to do?', reply_markup=reply_markup)
    return TRIP_TYPE

# Define the function for handling the trip type
def trip_type(update, context):

    query = update.callback_query

    trip_type = query.data
    if trip_type.lower() == "one-way":
        # query.message.edit_text("Please enter the departure date in the format dd/mm/yyyy (e.g. 01/05/2023):")

        context.user_data["trip_type"] = "one-way"
        query.message.edit_text("Please select a date: ",
            reply_markup=telegramcalendar.create_calendar())
    elif trip_type.lower() == "round trip":
        context.user_data["trip_type"] = "round trip"
        query.message.edit_text("Please select a date: ",
            reply_markup=telegramcalendar.create_calendar())
    else:
        query.message.reply_text("Sorry, I didn't understand. Please choose one-way or round trip.")
        return TRIP_TYPE
    return DEPARTURE



def departure(update, context):
    print(context)
    context.user_data["departure_date"] = update.message.text


    if context.user_data["trip_type"] == "one-way":
        keyboard = [
            [InlineKeyboardButton("Economy", callback_data='economy'),
             InlineKeyboardButton("Premium Economy", callback_data='premium economy')]
        ]
        reply_markup = InlineKeyboardMarkup(keyboard)
        update.message.reply_text("What class would you like to travel in?", reply_markup=reply_markup)
        return CLASS
    else:
        update.message.reply_text("Please enter the return date in the format dd/mm/yyyy (e.g. 01/05/2023):?")
        return RETURN

# Define the function for handling the return date
def return_date(update, context):
    context.user_data["return_date"] = update.message.text
    keyboard = [
        [InlineKeyboardButton("Economy", callback_data='economy'),
         InlineKeyboardButton("Premium Economy", callback_data='premium economy')]
    ]
    reply_markup = InlineKeyboardMarkup(keyboard)
    update.message.reply_text("What class would you like to travel in?", reply_markup=reply_markup)
    return CLASS

# Define the function for handling the class
def travel_class(update, context):
    context.user_data["class"] = update.callback_query.data
    update.callback_query.message.edit_text("How many passengers will be traveling?")
    return PASSENGERS

# Define the function for handling the number of passengers
def passengers(update, context):
    context.user_data["passengers"] = update.message.text
    update.message.reply_text("Thank you for your preferences! Here is a summary of your trip:\n\n"
                              f"Trip type: {context.user_data['trip_type']}\n"
                              f"Departure date: {context.user_data['departure_date']}\n"
                              f"Return date: {context.user_data.get('return_date', 'N/A')}\n"
                              f"Class: {context.user_data['class']}\n"
                              f"Number of passengers: {context.user_data['passengers']}\n\n")
    # End the conversation
    update.message.reply_text("Thank you for using the travel bot!")
    return ConversationHandler.END

# Define the function for handling canceling the conversation
def cancel(update, context):
    update.message.reply_text("The conversation has been cancelled.")
    return ConversationHandler.END

# Define the main function to run the bot
def main():
    # Set up the Telegram API
    bot_token = ""
    updater = Updater(bot_token, use_context=True)

    # Set up the conversation handler
    conv_handler = ConversationHandler(
        entry_points=[CommandHandler('start', start)],
        states={
            TRIP_TYPE: [CallbackQueryHandler(trip_type)],
            DEPARTURE: [MessageHandler(Filters.text & ~Filters.command, return_date)],
            # DEPARTURE: [MessageHandler(Filters.text & ~Filters.command, departure)],
            RETURN: [MessageHandler(Filters.text & ~Filters.command, return_date)],
            CLASS: [CallbackQueryHandler(travel_class)],
            PASSENGERS: [MessageHandler(Filters.text & ~Filters.command, passengers)]
        },
        fallbacks=[CommandHandler('cancel', cancel)]
    )
    # updater.dispatcher.add_handler(CallbackQueryHandler(inline_handler))
    # updater.dispatcher.add_handler(CallbackQueryHandler(calendar_handler))
    # Add the conversation handler to the updater
    updater.dispatcher.add_handler(conv_handler)

    # Start the bot
    updater.start_polling()
    updater.idle()

if __name__ == '__main__':
    main()

但是当代码到达这个函数时:

        query.message.edit_text("Please select a date: ",
        reply_markup=telegramcalendar.create_calendar())

我只得到日期选择器,它没有工作或接收到正确的回调数据。正如你在脚本中看到的那样,我已经尝试了很多东西,但似乎没有任何效果。

下面可以看到导入的日历脚本

#!/usr/bin/env python3
#
# A library that allows to create an inline calendar keyboard.
# grcanosa https://github.com/grcanosa
#
"""
Base methods for calendar keyboard creation and processing.
"""


from telegram import InlineKeyboardButton, InlineKeyboardMarkup,ReplyKeyboardRemove
import datetime
import calendar

def create_callback_data(action,year,month,day):
    """ Create the callback data associated to each button"""
    return ";".join([action,str(year),str(month),str(day)])

def separate_callback_data(data):
    print(data)
    """ Separate the callback data"""
    return data.split(";")


def create_calendar(year=None,month=None):
    """
    Create an inline keyboard with the provided year and month
    :param int year: Year to use in the calendar, if None the current year is used.
    :param int month: Month to use in the calendar, if None the current month is used.
    :return: Returns the InlineKeyboardMarkup object with the calendar.
    """
    now = datetime.datetime.now()
    if year == None: year = now.year
    if month == None: month = now.month
    data_ignore = create_callback_data("IGNORE", year, month, 0)
    keyboard = []
    #First row - Month and Year
    row=[]
    row.append(InlineKeyboardButton(calendar.month_name[month]+" "+str(year),callback_data=data_ignore))
    keyboard.append(row)
    #Second row - Week Days
    row=[]
    for day in ["Mo","Tu","We","Th","Fr","Sa","Su"]:
        row.append(InlineKeyboardButton(day,callback_data=data_ignore))
    keyboard.append(row)

    my_calendar = calendar.monthcalendar(year, month)
    for week in my_calendar:
        row=[]
        for day in week:
            if(day==0):
                row.append(InlineKeyboardButton(" ",callback_data=data_ignore))
            else:
                row.append(InlineKeyboardButton(str(day),callback_data=create_callback_data("DAY",year,month,day)))
        keyboard.append(row)
    #Last row - Buttons
    row=[]
    row.append(InlineKeyboardButton("<",callback_data=create_callback_data("PREV-MONTH",year,month,day)))
    row.append(InlineKeyboardButton(" ",callback_data=data_ignore))
    row.append(InlineKeyboardButton(">",callback_data=create_callback_data("NEXT-MONTH",year,month,day)))
    keyboard.append(row)

    return InlineKeyboardMarkup(keyboard)


def process_calendar_selection(bot,update):
    """
    Process the callback_query. This method generates a new calendar if forward or
    backward is pressed. This method should be called inside a CallbackQueryHandler.
    :param telegram.Bot bot: The bot, as provided by the CallbackQueryHandler
    :param telegram.Update update: The update, as provided by the CallbackQueryHandler
    :return: Returns a tuple (Boolean,datetime.datetime), indicating if a date is selected
                and returning the date if so.
    """
    ret_data = (False,None)
    query = bot.callback_query
    print(query.data)
    (action,year,month,day) = separate_callback_data(query.data)
    curr = datetime.datetime(int(year), int(month), 1)
    if action == "IGNORE":
        return
    elif action == "DAY":
        print("changing day")
        query.message.edit_text(query.message.text,
            )
        ret_data = True,datetime.datetime(int(year),int(month),int(day))
    elif action == "PREV-MONTH":
        pre = curr - datetime.timedelta(days=1)
        query.message.edit_text(query.message.text,
            reply_markup=create_calendar(int(pre.year),int(pre.month)))
    elif action == "NEXT-MONTH":
        ne = curr + datetime.timedelta(days=31)
        query.message.edit_text(query.message.text,
            reply_markup=create_calendar(int(ne.year),int(ne.month)))
    else:
        print("else")
        return
    return ret_data
python telegram python-telegram-bot
© www.soinside.com 2019 - 2024. All rights reserved.