Exemplo n.º 1
0
    def create_slack_response(api_response, verb):
        if (len(api_response) == 0):
            return SlackResponse.text('Server returned no data ¯\\_(ツ)_/¯')

        subset = CampMe.filter_items(api_response, verb)

        formatted = CampMe.format_text(subset, verb[0] == 'speaker')

        return SlackResponse.attachment(
            title=verb,
            text=formatted,
            author_name='Code Camp Bot',
            author_link='https://www.socalcodecamp.com/schedule.aspx')
Exemplo n.º 2
0
    def create_slack_response(self, api_response):
        if (api_response['total'] == 0):
            return SlackResponse.text('No taco ¯\\_(ツ)_/¯')

        found = random.choice(api_response['businesses'])

        description = 'Rated %s by %s people, is %s meters away' % (
            found['rating'], found['review_count'], int(found['distance']))

        return SlackResponse.attachment(title=found['name'],
                                        image_url=found['image_url'],
                                        text=description,
                                        author_name='Taco details...',
                                        author_link=found['url'])
Exemplo n.º 3
0
    def invoke(self, command, user):

        verb = self.get_verb(command)

        if (verb is None):
            return SlackResponse.text(self.USAGE_TEXT)

        if (verb[0] == 'help' or verb[0] == '?'):
            return SlackResponse.text(self.MANUAL)

        try:
            result = self.get_data(self.URL)
        except HTTPError as he:
            return SlackResponse.text(str(he))
        except URLError as ue:
            return SlackResponse.text(str(ue.args))

        return self.create_slack_response(result, verb)
Exemplo n.º 4
0
    def invoke(self, command, user):

        zip = self.get_zipcode(command)

        if (zip is None):
            return SlackResponse.text(
                r'Try: `tacome _ZIP_CODE_`. Example: `tacome 90405`')

        url = self.url + zip

        try:
            result = self.get_data(url)
        except HTTPError as he:
            return SlackResponse.text(str(he))
        except URLError as ue:
            return SlackResponse.text(str(ue.args))

        return self.create_slack_response(result)
Exemplo n.º 5
0
    def invoke(self, command, user):
        data = self.get_data()

        if data is None or data['results'] is None or len(data['results']) < 1:
            return SlackResponse.text('No results for ' + self.search_term)

        item = random.choice(data['results'])

        return TenorCommand.create_slack_response(item)
Exemplo n.º 6
0
    def invoke(self, command, user):
        parts = command.lower().split(' ')

        text = ''
        if len(parts) == 2:
            text = self.get_details(parts[1])
        else:
            text = self.get_listing()

        return SlackResponse.text(text=text)
Exemplo n.º 7
0
def test_handle_command_calls_slack_api_call_with_text(fake_aww_invoke,fake_slack_api_call):
    #arrange
    aww = Aww()
    commands[aww.get_command()] = aww
    user = "******"
    channel = "fake_channel"
    text, attachment = SlackResponse.text("bar")
    fake_aww_invoke.return_value = text, attachment
    #act
    handle_command("aww",channel,user)
    #assert
    fake_slack_api_call.assert_called_with('chat.postMessage', channel=channel, text=text,as_user=True)
Exemplo n.º 8
0
 def invoke(self, command, user):
     image, lat, lng, caption, date = self.get_data()
     image_url = "https://epic.gsfc.nasa.gov/epic-archive/jpg/" + image + ".jpg"
     maps_url = "https://www.google.com/maps/@" + lat + "," + lng + ",6z"
     pretext = caption
     text = "Taken on " + date
     author_name = lat + " " + lng + " (location map)"
     author_link = maps_url
     return SlackResponse.attachment(pretext=pretext,
                                     text=text,
                                     image_url=image_url,
                                     author_name=author_name,
                                     author_link=author_link)
Exemplo n.º 9
0
def test_handle_command_with_empty_string_does_not_call_slack(fake_aww_invoke,fake_slack_api_call):
    #arrange
    aww = Aww()
    commands[aww.get_command()] = aww
    user = "******"
    channel = "fake_channel"
    text, attachment = SlackResponse.text("bar")
    fake_aww_invoke.return_value = text, attachment
    #act
    handle_command("",channel,user)
    #assert
    assert not fake_slack_api_call.called
    assert not fake_aww_invoke.called
Exemplo n.º 10
0
Arquivo: cc.py Projeto: nurih/ccbot
    def invoke(self, input, user):
        self.bully_name = bot.get_user_name_by_id(self.bully_id)
        text = None
        command,action,args= self.parse_command(input)
        method_name = "action_" + action
        if method_name in dir(self):
            method_to_call = getattr(self, method_name)
            result = method_to_call(args)
            text = result
        else:
            if user == self.bully_id:
                result = random.choice(self.deny_response).format(name=self.bully_name, action=action)
                text = result

        return SlackResponse.text(text)
Exemplo n.º 11
0
def test_invoke_attachment_is_slack_attachment():
    #arrange
    api_client = ApiClient()
    api_client.fetch = MagicMock(return_value={
        'num': 123,
        'title': 'fake comic',
        'alt': 'fake alt',
        'img': 'fake_img'
    })
    xkcd = Xkcd()
    xkcd.api_client = api_client
    #act
    text, attachment = xkcd.invoke("xkcd", "fake_user")
    expected_text, expected_attachment = SlackResponse.attachment(
        title='fake comic', text='fake alt', image_url='fake_img')
    #assert
    assert attachment == expected_attachment
Exemplo n.º 12
0
    def invoke(self, input, user):
        info = self.fetch_info()
        last_comic_id = info["num"]
        command, args = self.parse_command(input)
        comic_id = None
        if (len(args) > 0):
            command = str(args[0])
            if command.isalpha() and command == "latest":
                comic_id = last_comic_id
            else:
                comic_id = args[0]

        random_comic = comic_id if comic_id is not None else random.randint(
            0, last_comic_id)
        comic_data = self.fetch_info(random_comic)
        return SlackResponse.attachment(title=comic_data["title"],
                                        text=comic_data["alt"],
                                        image_url=comic_data["img"])
Exemplo n.º 13
0
def test_invoke():
    #arrange
    e = EarthMe()
    e.get_info = MagicMock(
        return_value={
            'image': 'fake_img',
            'centroid_coordinates': {
                'lat': 1,
                'lon': 2
            },
            'caption': 'captionit',
            'date': '1/1/1111'
        })
    #act
    result = e.invoke("earthme", "fake_user")
    #assert
    assert result == SlackResponse.attachment(
        pretext='captionit',
        text="Taken on 1/1/1111",
        image_url="https://epic.gsfc.nasa.gov/epic-archive/jpg/fake_img.jpg",
        author_name="1 2 (location map)",
        author_link='https://www.google.com/maps/@1,2,6z')
Exemplo n.º 14
0
 def create_slack_response(item):
     return SlackResponse.attachment(title='Here you go:',
                              image_url=item['media'][0]['gif']['url'],
                              author_link=item['url'],
                              author_name='Via Tenor'
                              )
Exemplo n.º 15
0
Arquivo: catme.py Projeto: nurih/ccbot
 def invoke(self, command, user):
     data = self.get_data()
     image = random.choice(data).get("name","")
     image_url = "http://edgecats.net/cats/" + image
     return SlackResponse.attachment(title=image_url,image_url= image_url)