Ejemplo n.º 1
0
    async def gif_search(self, command, slack, registry):
        response = command.response()
        giphy = registry.get('giphy')

        if command.text:
            urls = await giphy.search(command.text)
            urls = [url.split('?')[0] for url in urls]

            att = Attachment(
                title='You searched for `{}`'.format(command.text),
                fallback='You searched for `{}`'.format(command.text),
                image_url=urls[0],
                callback_id='gif_search'
            )

            data = json.dumps(
                {'urls': urls, 'search': command.text, 'index': 0}
            )
            ok = Button(name='ok', text='Send', style='primary', value=data)
            next_ = Button(name='next', text='Next', value=data)

            att.actions = [ok, next_]
            response.attachments.append(att)
            await slack.send(response)
        else:
            url = giphy.trending()

            att = Attachment(
                title='Trending gif on giphy',
                fallback='Trending gif on giphy',
                image_url=url,
            )
            response.attachments.append(att)
            await slack.send(response)
Ejemplo n.º 2
0
    async def help_(self, message, slack, *_):
        response = message.response()
        response.text = 'Help of the good Sir-bot-a-lot'

        help_msg = Attachment(fallback='help',
                              color='good',
                              title='Common commands')
        hello_help = Field(title='Hello',
                           value='Say hello to Sir-bot-a-lot.'
                           '\n`@sir-bot-a-lot hello`',
                           short=True)
        admin_help = Field(title='Admin',
                           value='Send a message to the pythondev admin team.'
                           '\n `@sir-bot-a-lot admin ...`',
                           short=True)
        intro_doc_help = Field(title='Intro doc',
                               value='Link the intro doc.'
                               '\n `@sir-bot-a-lot intro doc`',
                               short=True)
        what_to_do_help = Field(title='What to do',
                                value='Link the what to do doc.'
                                '\n `@sir-bot-a-lot what to do`',
                                short=True)
        help_msg.fields.extend(
            (hello_help, admin_help, intro_doc_help, what_to_do_help))

        gif_help = Attachment(fallback='gif help',
                              color='good',
                              title='Gif commands')
        gif_random_help = Field(title='Random',
                                value='Get a random gif.'
                                '\n`@sir-bot-a-lot gif`',
                                short=True)
        gif_search_help = Field(title='Search',
                                value='Search for a gif.'
                                '\n`@sir-bot-a-lot gif search ...`',
                                short=True)
        gif_trending_help = Field(title='Trending',
                                  value='Get a trending gif.'
                                  '\n`@sir-bot-a-lot gif trending`',
                                  short=True)
        gif_by_id_help = Field(title='By ID',
                               value='''Get a gif by it's id.
                               \n`@sir-bot-a-lot gif <gif_id>`''',
                               short=True)
        gif_help.fields.extend((gif_random_help, gif_search_help,
                                gif_trending_help, gif_by_id_help))

        response.attachments.extend((help_msg, gif_help))
        await slack.send(response)
Ejemplo n.º 3
0
    async def pypi_search(self, command, slack, registry):
        response = command.response()
        pypi = registry.get('pypi')
        results = await pypi.search(command.text)

        if not command.text:
            response.text = 'Please enter the package name you wish to find'
            await slack.send(response)
            return

        if results:
            for result in results[:3]:
                att = Attachment(
                    title=result['name'],
                    fallback=result['name'],
                    text=result['summary'],
                    title_link='{}/{}'.format(pypi.ROOT_URL, result['name'])
                )
                response.attachments.append(att)

            if len(results) == 4:
                att = Attachment(
                    title=results[3]['name'],
                    fallback=results[3]['name'],
                    text=results[3]['summary'],
                    title_link='{}/{}'.format(
                        pypi.ROOT_URL, results[3]['name']
                    )
                )
                response.attachments.append(att)

            elif len(results) > 3:
                path = pypi.SEARCH_PATH.format(command.text)
                more_info = Attachment(
                    title='{0} more results..'.format(len(results) - 3),
                    fallback='{0} more results..'.format(len(results) - 3),
                    title_link='{}/{}'.format(pypi.ROOT_URL, path)
                )
                response.attachments.append(more_info)

            response.response_type = 'in_channel'
            response.text = "<@{}> Searched PyPi for `{}`".format(
                command.frm.id,
                command.text)
        else:
            response.text = "Could not find anything on PyPi matching" \
                            " `{0}`".format(command.text)

        await slack.send(response)
Ejemplo n.º 4
0
    async def send_pr_msg(self, pr, action, color):
        footer = '+ {add} / - {del_}'.format(
            add=pr.data['pull_request']['additions'],
            del_=pr.data['pull_request']['deletions'])

        att = Attachment(fallback='pull request {}'.format(action),
                         title='Pull request {action} in <{repo_url}|{name}>:'
                         ' <{url}|{title}>'.format(
                             repo_url=pr.data['repository']['html_url'],
                             url=pr.data['pull_request']['html_url'],
                             name=pr.data['repository']['name'],
                             action=pr.data['action'],
                             title=pr.data['pull_request']['title'],
                         ),
                         color=color,
                         text='*<{url}|{title}>*\n{body}'.format(
                             url=pr.data['pull_request']['html_url'],
                             title=pr.data['pull_request']['title'],
                             body=pr.data['pull_request']['body']),
                         author_icon=pr.data['sender']['avatar_url'],
                         author_name=pr.data['sender']['login'],
                         author_link=pr.data['sender']['html_url'],
                         footer=footer)

        slack = registry.get('slack')
        channel = await slack.channels.get(
            name=self.config['github']['channel'])
        msg = SlackMessage(to=channel)
        msg.attachments.append(att)
        await slack.send(msg)
Ejemplo n.º 5
0
    async def send_issue_msg(self, issue, color):
        att = Attachment(fallback='issue {}'.format(issue.data['action']),
                         color=color,
                         text='*<{url}|{title}>*\n{body}'.format(
                             url=issue.data['issue']['html_url'],
                             title=issue.data['issue']['title'],
                             body=issue.data['issue']['body']),
                         title='Issue {action} in <{repo_url}|{name}>'.format(
                             repo_url=issue.data['repository']['html_url'],
                             name=issue.data['repository']['name'],
                             action=issue.data['action'],
                         ),
                         author_icon=issue.data['sender']['avatar_url'],
                         author_name=issue.data['sender']['login'],
                         author_link=issue.data['sender']['html_url'],
                         footer=', '.join(
                             label['name']
                             for label in issue.data['issue']['labels']))

        slack = registry.get('slack')
        channel = await slack.channels.get(
            name=self.config['github']['channel'])
        msg = SlackMessage(to=channel)
        msg.attachments.append(att)
        await slack.send(msg)
Ejemplo n.º 6
0
    async def file(self, command, slack, *_):
        response = command.response()

        for file in self.config['files'].values():
            if command.text == file['match']:
                response.response_type = 'in_channel'
                response.text = self.config['files_template'].format(
                    file['url'], file['name'])
                break
        else:
            response.response_type = 'ephemeral'
            att = Attachment(title='Choose a file to show',
                             fallback='Choose a file to show',
                             callback_id='choose_file')

            data = [{
                'value': file['match'],
                'text': file['name']
            } for file in self.config['files'].values()]

            select = Select(name='choose_file',
                            text='Choose a file',
                            options=data)
            att.actions.append(select)
            response.attachments.append(att)

        await slack.send(response)
Ejemplo n.º 7
0
    async def share_admin(self, command, slack):

        response = command.response()
        to = await slack.groups.get('G1DRT62UC')

        att = Attachment(
            fallback='admin message',
            text=command.text,
            title='Message to the admin team by <@{}>'.format(command.frm.id),
        )

        response.to = to
        response.attachments.append(att)
        await slack.send(response)
Ejemplo n.º 8
0
    def _issue_format(self, event, color):
        att = Attachment(
            fallback='issue {}'.format(event['action']),
            color=color,
            text=event['issue']['body'],
            title='Issue {action} in <{repo_url}|{name}>:'
            ' <{url}|{title}>'.format(repo_url=event['repository']['html_url'],
                                      url=event['issue']['html_url'],
                                      name=event['repository']['name'],
                                      action=event['action'],
                                      title=event['issue']['title']),
            author_icon=event['sender']['avatar_url'],
            author_name=event['sender']['login'],
            author_link=event['sender']['html_url'],
            footer=', '.join(label['name']
                             for label in event['issue']['labels']))

        return att
Ejemplo n.º 9
0
    async def leaderboard(self, command, slack):
        response = command.response()

        candy = registry.get('candy')
        data = await candy.top(count=10)
        att = Attachment(
            title='{} Leaderboard'.format(self.config['candy']['trigger']),
            fallback='{} Leaderboard'.format(self.config['candy']['trigger']),
            color='good')

        if not data:
            response.text = 'No data to display'
        else:
            response.response_type = 'in_channel'
            for user in data:
                slack_user = await slack.users.get(user['user'])
                att.text += '{} *{}*\n'.format(slack_user.name, user['candy'])

            response.attachments.append(att)

        await slack.send(response)
Ejemplo n.º 10
0
    def _pull_request_format(self, event, data):
        footer = '+ {add} / - {del_}'.format(
            add=event['pull_request']['additions'],
            del_=event['pull_request']['deletions'])

        att = Attachment(fallback='pull request {}'.format(data['action']),
                         title='Pull request {action} in <{repo_url}|{name}>:'
                         ' <{url}|{title}>'.format(
                             repo_url=event['repository']['html_url'],
                             url=event['pull_request']['html_url'],
                             name=event['repository']['name'],
                             action=data['action'],
                             title=event['pull_request']['title'],
                         ),
                         color=data['color'],
                         text=event['pull_request']['body'],
                         author_icon=event['sender']['avatar_url'],
                         author_name=event['sender']['login'],
                         author_link=event['sender']['html_url'],
                         footer=footer)

        return att
Ejemplo n.º 11
0
    async def gif_search_action(self, action, slack):
        response = action.response()
        data = json.loads(action.action['value'])

        if action.action['name'] == 'ok':
            title = '<@{}> Searched giphy for: `{}`'.format(
                action.frm.id, data['search'])

            att = Attachment(
                title=title,
                fallback=title,
                image_url=data['urls'][data['index']],
            )

            response.attachments.append(att)
            response.replace_original = False
            await slack.send(response)

            confirm = action.response()
            confirm.text = 'Gif successfully sent'
            await slack.send(confirm)

        elif action.action['name'] in ('next', 'previous'):

            if action.action['name'] == 'next':
                index = data['index'] + 1
            else:
                index = data['index'] - 1

            url = data['urls'][index]

            att = Attachment(title='Giphy search: `{}`'.format(data['search']),
                             fallback='Giphy search: `{}`'.format(
                                 data['search']),
                             image_url=url,
                             callback_id='gif_search')

            data['index'] = index
            data_json = json.dumps(data)

            ok = Button(name='ok',
                        text='Send',
                        style='primary',
                        value=data_json)
            att.actions.append(ok)

            if index != 0:
                previous = Button(name='previous',
                                  text='Previous',
                                  value=data_json)
                att.actions.append(previous)

            if len(data['urls']) > index + 1:
                next_ = Button(name='next', text='Next', value=data_json)
                att.actions.append(next_)

            response.attachments.append(att)
            await slack.send(response)

        else:
            return