Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import logging
- from telegram.ext import Updater, CommandHandler, CallbackQueryHandler, MessageHandler, Filters, ConversationHandler
- from config import bot_token
- from model import User, sqlite_db, IntegrityError
- # from keyboards import _, user_language, available_locales, inline_language_menu, reply_start_menu, inline_cancel_menu
- from language import _, user_language
- from keyboards import *
- # ================================================================================
- logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
- level=logging.INFO)
- logger = logging.getLogger(__name__)
- def get_user_data(update):
- try:
- with sqlite_db.atomic():
- user = User.get(User.id == update.effective_user.id)
- return user
- except IntegrityError:
- return "Cannot get user's data"
- def start(update, context):
- try:
- with sqlite_db.atomic():
- User.create(id=update.effective_user.id)
- return welcome(update, context)
- except IntegrityError:
- user = get_user_data(update)
- if user:
- return welcome(update, context)
- @user_language
- def welcome(update, context):
- """Send a message when the command /start is issued."""
- user = get_user_data(update)
- if not user.is_registered:
- return update.effective_message.reply_text(text="Choose language", reply_markup=inline_language_menu())
- else:
- # print(context.user_data)
- update.effective_message.reply_text(_("msg_welcome"), reply_markup=reply_start_menu())
- @user_language
- def show_help(update, context):
- """Send a message when the command /help is issued."""
- update.effective_message.reply_text(text=_('msg_help_menu'))
- update.effective_message.reply_text(text=_('msg_text_params', params=[update.effective_user.id, update.effective_message.text]))
- @user_language
- def change_lang(update, context):
- """Change user language."""
- return update.effective_message.reply_text(text=_("msg_lang_selection"), reply_markup=inline_language_menu())
- def drop_tables(update, context):
- pass
- # @user_language
- def callback_handler(update, context):
- data = update.callback_query
- user = get_user_data(update)
- for locale, description in sorted(available_locales.items()):
- if data.data == "lang_"+locale:
- user.update(lang=locale).execute()
- user.update(is_registered=True).execute()
- @user_language
- def sendLangMes(update, context):
- data.edit_message_text(text=_('msg_lang_selection_s', params=[description]))
- return welcome(update, context)
- return sendLangMes(update, context)
- if data.data == "cancel_login":
- welcome(update, context)
- update.effective_message.delete()
- del context.user_data['login']
- del context.user_data['password']
- return ConversationHandler.END
- TYPING_LOGIN, TYPING_PASS = range(2)
- @user_language
- def go_menu_settings(update, context):
- update.effective_message.reply_text(_("cmd_settings"), reply_markup=reply_settings_menu())
- @user_language
- def go_menu_timetable(update, context):
- print(context.user_data)
- update.effective_message.reply_markdown(_("msg_credentials_login"), reply_markup=inline_cancel_menu())
- return TYPING_LOGIN
- @user_language
- def get_auth_login(update, context):
- message_id = update.effective_message.message_id-1
- chat_id = update.effective_message.chat_id
- login = update.effective_message.text
- context.user_data['login'] = login
- context.bot.delete_message(chat_id=chat_id, message_id=message_id)
- update.effective_message.reply_markdown(text=_("msg_credentials_pass", params=[context.user_data['login']]), reply_markup=inline_cancel_menu())
- return TYPING_PASS
- @user_language
- def get_auth_pass(update, context):
- message_id = update.effective_message.message_id - 1
- chat_id = update.effective_message.chat_id
- context.user_data['password'] = update.effective_message.text
- context.bot.delete_message(chat_id=chat_id, message_id=message_id)
- text = _("msg_credentials_is_correct", params=[context.user_data['login'], context.user_data['password']])
- update.effective_message.reply_markdown(text, reply_markup=inline_cancel_menu())
- return ConversationHandler.END
- @user_language
- def get_authorization(update, context):
- update.effective_message.reply_markdown(update.effective_message.text, reply_markup=inline_cancel_menu())
- @user_language
- def get_save_confirmation(update, context):
- update.effective_message.reply_markdown("Conv end", reply_markup=inline_cancel_menu())
- # def show_time
- def error(update, context):
- """Log Errors caused by Updates."""
- update.effective_message.reply_text(f'Update caused error "{context.error}"')
- def main():
- """Start the bot."""
- updater = Updater(bot_token, use_context=True)
- dp = updater.dispatcher
- dp.add_handler(CommandHandler("start", start))
- dp.add_handler(CommandHandler("help", show_help))
- dp.add_handler(CommandHandler("language", change_lang))
- dp.add_handler(CallbackQueryHandler(callback_handler))
- auth_conv_handler = ConversationHandler(
- entry_points=[MessageHandler(Filters.regex(_("cmd_timetable", locale="all")), go_menu_timetable, pass_user_data=True)],
- states= {
- TYPING_LOGIN:[MessageHandler(Filters.text, get_auth_login, pass_user_data=True)],
- TYPING_PASS:[MessageHandler(Filters.text, get_auth_pass, pass_user_data=True)]
- },
- fallbacks=[CallbackQueryHandler(callback_handler)]
- )
- dp.add_handler(auth_conv_handler)
- dp.add_handler(MessageHandler(Filters.regex(_("cmd_settings", locale="all")), go_menu_settings))
- dp.add_handler(MessageHandler(Filters.regex(_("cmd_language", locale="all")), change_lang))
- dp.add_error_handler(error)
- updater.start_polling()
- updater.idle()
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement