I need a telegram bot that allows me to select two dates, after having chosen a name. This is my code I'm new at this, I hope you can help me, I'm using it python-telegram-bot
The problem with the code is that it only allows me to choose the first date and then the calendar disappears.
My code is based in https://github.com/grcanosa/telegram-calendar-keyboard
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import logging
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import Updater, CommandHandler, CallbackQueryHandler,ConversationHandler
from telegram.ext import CallbackQueryHandler
from telegram import ReplyKeyboardRemove
import telegramcalendar
FIRST, SECOND = range(2)
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
logger = logging.getLogger(__name__)
def calendar_handler(bot,update):
query = update.callback_query
reply_markup=telegramcalendar.create_calendar()
bot.edit_message_text(
chat_id=query.message.chat_id,
message_id=query.message.message_id,
text=u"Choose First date"
)
bot.edit_message_reply_markup(
chat_id=query.message.chat_id,
message_id=query.message.message_id,
reply_markup=reply_markup
)
selected,date = telegramcalendar.process_calendar_selection(bot, update)
if selected:
bot.send_message(chat_id=update.callback_query.from_user.id,
text="Date: %s" % (date.strftime("%Y-%m-%d")))
return SECOND
def calendar_handler1(bot,update):
query = update.callback_query
reply_markup=telegramcalendar.create_calendar()
bot.edit_message_text(
chat_id=query.message.chat_id,
message_id=query.message.message_id,
text=u"Choose second date"
)
bot.edit_message_reply_markup(
chat_id=query.message.chat_id,
message_id=query.message.message_id,
reply_markup=reply_markup
)
selected1,date = telegramcalendar.process_calendar_selection(bot, update)
if selected1:
bot.send_message(chat_id=update.callback_query.from_user.id,
text="Other date: %s" % (date.strftime("%Y-%m-%d")))
def start(bot, update):
keyboard = [[InlineKeyboardButton("Name1", callback_data='1'),
InlineKeyboardButton("Name2", callback_data='2')]]
reply_markup = InlineKeyboardMarkup(keyboard)
update.message.reply_text('Choose:', reply_markup=reply_markup)
return FIRST
def main():
updater = Updater("TOKEN")
conv_handler = ConversationHandler(
entry_points=[CommandHandler('start', start)],
states={
FIRST: [CallbackQueryHandler(calendar_handler)],
SECOND: [CallbackQueryHandler(calendar_handler1)]
},
fallbacks=[CommandHandler('start', start)]
)
updater.dispatcher.add_handler(conv_handler)
# Start the Bot
updater.start_polling()
# Run the bot until the user presses Ctrl-C or the process receives SIGINT,
# SIGTERM or SIGABRT
updater.idle()
if __name__ == '__main__':
main()