Advertisement
anvarkhamidov

Untitled

Oct 2nd, 2019
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.00 KB | None | 0 0
  1. import logging
  2. from telegram.ext import Updater, CommandHandler, CallbackQueryHandler, MessageHandler, Filters, ConversationHandler
  3. from config import bot_token
  4. from model import User, sqlite_db, IntegrityError
  5. # from keyboards import _, user_language, available_locales, inline_language_menu, reply_start_menu, inline_cancel_menu
  6. from language import _, user_language
  7. from keyboards import *
  8. # ================================================================================
  9.  
  10. logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
  11.                     level=logging.INFO)
  12.  
  13. logger = logging.getLogger(__name__)
  14.  
  15.  
  16. def get_user_data(update):
  17.     try:
  18.         with sqlite_db.atomic():
  19.             user = User.get(User.id == update.effective_user.id)
  20.             return user
  21.     except IntegrityError:
  22.         return "Cannot get user's data"
  23.  
  24.  
  25. def start(update, context):
  26.     try:
  27.         with sqlite_db.atomic():
  28.             User.create(id=update.effective_user.id)
  29.             return welcome(update, context)
  30.     except IntegrityError:
  31.         user = get_user_data(update)
  32.         if user:
  33.             return welcome(update, context)
  34.  
  35. @user_language
  36. def welcome(update, context):
  37.     """Send a message when the command /start is issued."""
  38.     user = get_user_data(update)
  39.     if not user.is_registered:
  40.         return update.effective_message.reply_text(text="Choose language", reply_markup=inline_language_menu())
  41.     else:
  42.         # print(context.user_data)
  43.         update.effective_message.reply_text(_("msg_welcome"), reply_markup=reply_start_menu())
  44.  
  45.  
  46. @user_language
  47. def show_help(update, context):
  48.     """Send a message when the command /help is issued."""
  49.     update.effective_message.reply_text(text=_('msg_help_menu'))
  50.     update.effective_message.reply_text(text=_('msg_text_params', params=[update.effective_user.id, update.effective_message.text]))
  51.  
  52. @user_language
  53. def change_lang(update, context):
  54.     """Change user language."""
  55.     return update.effective_message.reply_text(text=_("msg_lang_selection"), reply_markup=inline_language_menu())
  56.  
  57. def drop_tables(update, context):
  58.     pass
  59.  
  60.  
  61. # @user_language
  62. def callback_handler(update, context):
  63.     data = update.callback_query
  64.     user = get_user_data(update)
  65.     for locale, description in sorted(available_locales.items()):
  66.         if data.data == "lang_"+locale:
  67.             user.update(lang=locale).execute()
  68.             user.update(is_registered=True).execute()
  69.             @user_language
  70.             def sendLangMes(update, context):
  71.                 data.edit_message_text(text=_('msg_lang_selection_s', params=[description]))
  72.                 return welcome(update, context)
  73.             return sendLangMes(update, context)
  74.     if data.data == "cancel_login":
  75.         welcome(update, context)
  76.         update.effective_message.delete()
  77.         del context.user_data['login']
  78.         del context.user_data['password']
  79.         return ConversationHandler.END
  80.  
  81.  
  82. TYPING_LOGIN, TYPING_PASS = range(2)
  83.  
  84. @user_language
  85. def go_menu_settings(update, context):
  86.     update.effective_message.reply_text(_("cmd_settings"), reply_markup=reply_settings_menu())
  87.  
  88. @user_language
  89. def go_menu_timetable(update, context):
  90.     print(context.user_data)
  91.     update.effective_message.reply_markdown(_("msg_credentials_login"), reply_markup=inline_cancel_menu())
  92.     return TYPING_LOGIN
  93.  
  94. @user_language
  95. def get_auth_login(update, context):
  96.     message_id = update.effective_message.message_id-1
  97.     chat_id = update.effective_message.chat_id
  98.     login = update.effective_message.text
  99.     context.user_data['login'] = login
  100.     context.bot.delete_message(chat_id=chat_id, message_id=message_id)
  101.     update.effective_message.reply_markdown(text=_("msg_credentials_pass", params=[context.user_data['login']]), reply_markup=inline_cancel_menu())
  102.     return TYPING_PASS
  103.  
  104. @user_language
  105. def get_auth_pass(update, context):
  106.     message_id = update.effective_message.message_id - 1
  107.     chat_id = update.effective_message.chat_id
  108.     context.user_data['password'] = update.effective_message.text
  109.     context.bot.delete_message(chat_id=chat_id, message_id=message_id)
  110.     text = _("msg_credentials_is_correct", params=[context.user_data['login'], context.user_data['password']])
  111.     update.effective_message.reply_markdown(text, reply_markup=inline_cancel_menu())
  112.     return ConversationHandler.END
  113.  
  114. @user_language
  115. def get_authorization(update, context):
  116.     update.effective_message.reply_markdown(update.effective_message.text, reply_markup=inline_cancel_menu())
  117.  
  118. @user_language
  119. def get_save_confirmation(update, context):
  120.     update.effective_message.reply_markdown("Conv end", reply_markup=inline_cancel_menu())
  121.  
  122. # def show_time
  123.  
  124. def error(update, context):
  125.     """Log Errors caused by Updates."""
  126.     update.effective_message.reply_text(f'Update caused error "{context.error}"')
  127.  
  128.  
  129.  
  130. def main():
  131.     """Start the bot."""
  132.     updater = Updater(bot_token, use_context=True)
  133.     dp = updater.dispatcher
  134.     dp.add_handler(CommandHandler("start", start))
  135.     dp.add_handler(CommandHandler("help", show_help))
  136.     dp.add_handler(CommandHandler("language", change_lang))
  137.     dp.add_handler(CallbackQueryHandler(callback_handler))
  138.     auth_conv_handler = ConversationHandler(
  139.         entry_points=[MessageHandler(Filters.regex(_("cmd_timetable", locale="all")), go_menu_timetable, pass_user_data=True)],
  140.         states= {
  141.             TYPING_LOGIN:[MessageHandler(Filters.text, get_auth_login, pass_user_data=True)],
  142.             TYPING_PASS:[MessageHandler(Filters.text, get_auth_pass, pass_user_data=True)]
  143.         },
  144.         fallbacks=[CallbackQueryHandler(callback_handler)]
  145.     )
  146.     dp.add_handler(auth_conv_handler)
  147.     dp.add_handler(MessageHandler(Filters.regex(_("cmd_settings", locale="all")), go_menu_settings))
  148.     dp.add_handler(MessageHandler(Filters.regex(_("cmd_language", locale="all")), change_lang))
  149.     dp.add_error_handler(error)
  150.     updater.start_polling()
  151.     updater.idle()
  152.  
  153.  
  154. if __name__ == '__main__':
  155.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement