Example #1
0
class LastFMSetCommand(Command, CreateOrUpdateMixin):
    """
    Command /lastfmset
    Sets the given Last.fm username to the current user
    """
    COMMAND = 'lastfmset'

    def __init__(self, update: Update, context: CallbackContext):
        super().__init__(update, context)
        self.telegram_api_client = TelegramAPIClient()
        self.lastfm_api_client = LastfmAPIClient()

    def get_response(self):
        # We call save_user() because we want to ensure
        # that the Telegram User already exists in the API Database
        self.save_user(self.update.message.from_user)

        lastfm_username = self._set_lastfm_username(self.update.message.from_user)
        return self._build_message(lastfm_username), None

    def _build_message(self, lastfm_username):
        if not lastfm_username:
            return self._help_message()
        return f"<strong>{lastfm_username}</strong>'s Last.fm username set correctly"

    def _set_lastfm_username(self, user):
        if not self.args:
            return None
        username = self.args[0]
        username = username.replace('@', '')
        user = self.telegram_api_client.create_user(user)
        lastfm_user = self.lastfm_api_client.set_lastfm_user(user.get('id'), username)
        return lastfm_user.get('username')

    @staticmethod
    def _help_message():
        return 'Command usage: /lastfmset username'
Example #2
0
 def save_user(user):
     telegram_api_client = TelegramAPIClient()
     create_user_response = telegram_api_client.create_user(user)
     return create_user_response