예제 #1
0
def new_game(bot, update):
    user = User.get_or_none(User.id == update.message.from_user.id)

    if not user:
        update.message.reply_text('You are not registered!')
        return ConversationHandler.END

    games = gmr.get_games(user.steam_id, user.authorization_key)

    if len(games) == 0:
        update.message.reply_text('You are not part of any games')
        return ConversationHandler.END

    games = list(
        filter(
            lambda x: not Game.select().where(Game.id == x['GameId']).exists(),
            games
        )
    )

    if len(games) == 0:
        update.message.reply_text('No games to be added')
        return ConversationHandler.END

    custom_keyboard = []
    for game in games:
        custom_keyboard.append([game['Name']])
    custom_keyboard.append(['cancel'])
    reply_markup = telegram.ReplyKeyboardMarkup(custom_keyboard)

    update.message.reply_text('Chose the game', reply_markup=reply_markup)

    return SELECT
예제 #2
0
def start_message(bot, update):
    user = User.get_or_none(User.id == update.message.from_user.id)

    msg = 'Welcome to telegram bot 2.0'

    if not user:
        msg += '\nRun /register to get started'
    update.message.reply_text(msg)
예제 #3
0
def add_game(bot, update):
    user = User.get_or_none(User.id == update.message.from_user.id)

    if not user:
        update.message.reply_text('You are not registered!')
        return ConversationHandler.END

    chat_id = update.message.chat_id
    if update.message.chat.type != 'private':
        admin_ids = [
            admin.user.id for admin in bot.get_chat_administrators(chat_id)
        ]
        if update.message.from_user.id not in admin_ids:
            update.message.reply_text('You are not admin of the group!')
            return ConversationHandler.END

    games = user.games

    if len(games) == 0:
        update.message.reply_text("You don't have any registered games")
        return ConversationHandler.END

    games = list(
        filter(
            lambda g: not (
                Subscription.select().where(
                    Subscription.game == g
                ).where(
                    Subscription.chat_id == chat_id
                ).exists()
            ),
            games
        )
    )

    if len(games) == 0:
        update.message.reply_text(
            "You don't have any registered games not in this chat"
        )
        return ConversationHandler.END

    games = list(filter(lambda g: g.active, games))

    if len(games) == 0:
        update.message.reply_text("You don't have any active games")
        return ConversationHandler.END

    custom_keyboard = []
    for game in games:
        custom_keyboard.append([game.name])
    custom_keyboard.append(['cancel'])
    reply_markup = telegram.ReplyKeyboardMarkup(custom_keyboard)

    update.message.reply_text('Chose the game', reply_markup=reply_markup)

    return SELECT
예제 #4
0
def register(bot, update):
    user = User.get_or_none(User.id == update.message.from_user.id)

    if user:
        update.message.reply_text("You are already registered!")
        return ConversationHandler.END

    update.message.reply_text("Provide GMR Authentication key"
                              "Authentication key can be acquired "
                              "from http://multiplayerrobot.com/Download")

    return AUTHKEY
예제 #5
0
def select_game(bot, update):
    if update.message.text == 'cancel':
        update.message.reply_text(
            'Canceled',
            reply_markup=telegram.ReplyKeyboardRemove()
        )
        return ConversationHandler.END

    user = User.get_or_none(User.id == update.message.from_user.id)

    games = gmr.get_games(user.steam_id, user.authorization_key)
    games_data = [g for g in games if g['Name'] == update.message.text]

    if len(games_data) == 0:
        update.message.reply_text(
            'Game does not exist',
            reply_markup=telegram.ReplyKeyboardRemove()
        )
        return ConversationHandler.END
    game_data = games_data[0]

    if Game.select().where(Game.id == game_data['GameId']).exists():
        update.message.reply_text(
            'Game already registered',
            reply_markup=telegram.ReplyKeyboardRemove()
        )
        return ConversationHandler.END

    game = Game.create(
        id=game_data['GameId'],
        owner=user,
        name=game_data['Name'],
        current_steam_id=game_data['CurrentTurn']['UserId']
    )

    for player in game_data['Players']:
        Player.create(
            steam_id=player['UserId'],
            game=game,
            order=player['TurnOrder']
        )

    update.message.reply_text(
        f'Game {game.name} registered',
        reply_markup=telegram.ReplyKeyboardRemove()
    )

    return ConversationHandler.END
예제 #6
0
def unregister(bot, update):
    user = User.get_or_none(User.id == update.message.from_user.id)

    if not user:
        update.message.reply_text("You are not registered!")
        return ConversationHandler.END

    custom_keyboard = [['Yes'], ['No']]
    reply_markup = telegram.ReplyKeyboardMarkup(custom_keyboard)

    update.message.reply_text(
        "Are you sure you want to unregister."
        "You will stop receiving notifications from current games."
        "Your steam id is still kept in order for active games to function."
        "You can register back anytime to continue receiving notifications.",
        reply_markup=reply_markup)
    return VERIFY
예제 #7
0
def select_game(bot, update):
    if update.message.text == 'cancel':
        update.message.reply_text(
            'Canceled',
            reply_markup=telegram.ReplyKeyboardRemove()
        )
        return ConversationHandler.END

    user = User.get_or_none(User.id == update.message.from_user.id)

    game = [g for g in user.games if g.name == update.message.text]

    if len(game) == 0:
        update.message.reply_text(
            'Game does not exist',
            reply_markup=telegram.ReplyKeyboardRemove()
        )
        return ConversationHandler.END
    game = game[0]

    chat_id = update.message.chat_id
    subscriptions = Subscription.select().where(
            Subscription.game == game
        ).where(
            Subscription.chat_id == chat_id
        )
    if subscriptions.exists():
        update.message.reply_text(
            'Game has already been added',
            reply_markup=telegram.ReplyKeyboardRemove()
        )
        return ConversationHandler.END

    Subscription.create(
        game=game,
        chat_id=chat_id
    )

    update.message.reply_text(
        f'Subscribed to {game.name}.'
        f' This chat will now start receiving notifications for the '
        'game. To get notifications, send /register to me as private message',
        reply_markup=telegram.ReplyKeyboardRemove())

    return ConversationHandler.END
    def test_verify_should_remove_user_if_yes(self, mock_keyboard):
        database = SqliteDatabase(':memory:')
        database_proxy.initialize(database)
        database.create_tables([User])
        User.create(id=111, steam_id=0, authorization_key='')

        bot_mock = Mock()
        update_mock = Mock()
        update_mock.message.from_user.id = 111
        update_mock.message.text = 'Yes'
        mock_keyboard.return_value = mock_keyboard

        self.assertEqual(ConversationHandler.END, cmd_unregister.verify(bot_mock, update_mock))
        update_mock.message.reply_text.assert_called_with(
            "Unregistered, your user data was removed!",
            reply_markup=mock_keyboard
        )
        self.assertIsNone(User.get_or_none(User.id == 111))
예제 #9
0
    def test_authkey_should_success_and_create_user(self, mock_gmr):
        database = SqliteDatabase(':memory:')
        database_proxy.initialize(database)
        database.create_tables([User])

        bot_mock = Mock()
        update_mock = Mock()
        update_mock.message.from_user.id = 111
        update_mock.message.text = 'auth_key'

        self.assertEqual(ConversationHandler.END,
                         cmd_register.authkey(bot_mock, update_mock))

        update_mock.message.reply_text.assert_called_with(
            "Successfully registered with steam id 123123")
        mock_gmr.assert_called_with('auth_key')

        user = User.get_or_none(User.id == 111)
        self.assertIsNotNone(user)
        self.assertEqual(123123, user.steam_id)
        self.assertEqual('auth_key', user.authorization_key)
예제 #10
0
    def test_authkey_should_retry_if_fail(self, mock_gmr):
        database = SqliteDatabase(':memory:')
        database_proxy.initialize(database)
        database.create_tables([User])

        bot_mock = Mock()

        update_mock = Mock()
        update_mock.message.from_user.id = 232
        update_mock.message.text = 'auth_key'

        mock_gmr.side_effect = InvalidAuthKey

        self.assertEqual(cmd_register.AUTHKEY,
                         cmd_register.authkey(bot_mock, update_mock))

        update_mock.message.reply_text.assert_called_with(
            "Authkey incorrect, try again (/cancel to end)")
        mock_gmr.assert_called_with('auth_key')

        user = User.get_or_none(User.id == 232)
        self.assertIsNone(user)