Esempio n. 1
0
    def send_card(self,
                  body: str = '',
                  to: Identifier = None,
                  in_reply_to: Message = None,
                  summary: str = None,
                  title: str = '',
                  link: str = None,
                  image: str = None,
                  thumbnail: str = None,
                  color: str = 'green',
                  fields: Tuple[Tuple[str, str], ...] = ()) -> None:
        """
        Sends a card.

        A Card is a special type of preformatted message. If it matches with a backend similar concept like on
        Slack or Hipchat it will be rendered natively, otherwise it will be sent as a regular formatted message.

        :param body: main text of the card in markdown.
        :param to: the card is sent to this identifier (Room, RoomOccupant, Person...).
        :param in_reply_to: the original message this message is a reply to (optional).
        :param summary: (optional) One liner summary of the card, possibly collapsed to it.
        :param title: (optional) Title possibly linking.
        :param link: (optional) url the title link is pointing to.
        :param image: (optional) link to the main image of the card.
        :param thumbnail: (optional) link to an icon / thumbnail.
        :param color: (optional) background color or color indicator.
        :param fields: (optional) a tuple of (key, value) pairs.
        """
        frm = in_reply_to.to if in_reply_to else self.bot_identifier
        if to is None:
            if in_reply_to is None:
                raise ValueError('Either to or in_reply_to needs to be set.')
            to = in_reply_to.frm
        self._bot.send_card(Card(body, frm, to, in_reply_to, summary, title, link, image, thumbnail, color, fields))
Esempio n. 2
0
    def send_text_with_buttons(self, recipient_id, message, buttons):
        if len(buttons) > 5:
            logging.warn("Slack API currently allows only up to 5 buttons."
                         "If you add more, all will be ignored.")
            return self.send_text_message(recipient_id, message)
        card = Card(summary=message,
                    title="Nachricht vom {}".format(self.bot.bot_identifier),
                    to=self._evaluate_identifier_by_recipient_id(recipient_id),
                    fields=self._convert_to_slack_buttons(buttons))

        self._send_to_card_or_text(card, recipient_id)
    def send_slack_attachment_action(self,
                                     body: str = '',
                                     to: Identifier = None,
                                     in_reply_to: Message = None,
                                     summary: str = None,
                                     title: str = '',
                                     link: str = None,
                                     image: str = None,
                                     thumbnail: str = None,
                                     color: str = 'green',
                                     fields: Tuple[Tuple[str, str], ...] = (),
                                     callback_id: str = None,
                                     fallback: str = None,
                                     actions=[]) -> None:
        """
        send attachment message.
        this code is customized from errbot.botplugin and errbot.backends.slack
        """

        frm = in_reply_to.to if in_reply_to else self.bot_identifier
        if to is None:
            if in_reply_to is None:
                raise ValueError('Either to or in_reply_to needs to be set.')
            to = in_reply_to.frm

        if isinstance(to, RoomOccupant):
            to = to.room
        to_humanreadable, to_channel_id = self._bot._prepare_message(
            Card(body, frm, to, in_reply_to, summary, title, link, image,
                 thumbnail, color, fields))
        attachment = {}
        if actions:
            attachment['actions'] = actions
        if callback_id:
            attachment['callback_id'] = callback_id

        if summary:
            attachment['pretext'] = summary
        if title:
            attachment['title'] = title
        if link:
            attachment['title_link'] = link
        if image:
            attachment['image_url'] = image
        if thumbnail:
            attachment['thumb_url'] = thumbnail
        if fallback:
            attachment['fallback'] = fallback
        attachment['text'] = body

        if color:
            attachment['color'] = self.COLORS[color] if color in self.COLORS else color

        if fields:
            attachment['fields'] = [{
                'title': key,
                'value': value,
                'short': True
            } for key, value in fields]

        data = {
            'text': ' ',
            'channel': to_channel_id,
            'attachments': json.dumps([attachment]),
            'link_names': '1',
            'as_user': '******'
        }
        try:
            self.log.debug('Sending data:\n%s', data)
            self._bot.api_call('chat.postMessage', data=data)
        except Exception as e:
            self.log.exception(
                "An exception occurred while trying to send a card to %s.[%s]"
                % (to_humanreadable, data))