Exemplo n.º 1
0
def get_targeted_user_id(args: list, command, response_channel):
    if len(args) == 1:
        targeted_user_name = args[0]
        #TODO: Remove megatron user
        command_url = MegatronUser.objects.first().command_url
        response = requests.post(command_url,
                                 json={
                                     'megatron_verification_token':
                                     settings.MEGATRON_VERIFICATION_TOKEN,
                                     'command': 'search_user',
                                     'targeted_user_name': targeted_user_name
                                 })
        potential_users = response.json()['users']
        if not potential_users:
            raise MegatronException(
                {'text': "I really don't know who that is. Try again?"})
        elif len(potential_users) == 1:
            targeted_user = potential_users[0]
        else:
            response = _disambiguate_response(potential_users, command)
            raise MegatronException(response)

    elif len(args) == 0:
        targeted_user = utils.get_customer_for_megatron_channel(
            response_channel)
        if targeted_user is None:
            raise MegatronException(
                {'text': 'Please specify a user for this command.'})

    else:
        raise MegatronException(
            {'text': "Too many arguments to command. Expecting two."})

    return {'targeted_platform_id': targeted_user['platform_user_id']}
Exemplo n.º 2
0
def require_targeted_user_id(args: list, command, response_channel):
    try:
        return get_targeted_user_id(args, command, response_channel)
    except KeyError:
        raise MegatronException(
            {"text": "Give me a little help! Who do you want to speak with?"}
        )
Exemplo n.º 3
0
 def open_im(self, slack_user_id: str):
     open_im_data = {'token': self.token, 'user': slack_user_id}
     open_response = safe_requests.post(OPEN_IM_URL, open_im_data)
     open_response_data = open_response.json()
     if not open_response_data['ok']:
         raise MegatronException(
             "Could not open DM channel with user: {}.  Error: {}".format(
                 slack_user_id, open_response_data['error']))
     return open_response_data
Exemplo n.º 4
0
 def im_history(self, channel_id: str, count: int):
     im_history_data = {
         'token': self.token,
         'channel': channel_id,
         'count': count,
     }
     response = safe_requests.post(IM_HISTORY_URL, im_history_data)
     response_data = response.json()
     if not response_data['ok']:
         raise MegatronException(
             ("Could not retrieve history for DM channel: {}. "
              "Error: {}").format(channel_id, response_data['error']))
     return response_data
Exemplo n.º 5
0
 def im_history(self, channel_id: str, count: int):
     im_history_data = {
         "token": self.token,
         "channel": channel_id,
         "count": count
     }
     response = safe_requests.post(IM_HISTORY_URL, im_history_data)
     response_data = response.json()
     if not response_data["ok"]:
         raise MegatronException(
             ("Could not retrieve history for DM channel: {}. Error: {}"
              ).format(channel_id, response_data["error"]))
     return response_data