hubot-mattermost icon indicating copy to clipboard operation
hubot-mattermost copied to clipboard

sending Message Attachments

Open akki005 opened this issue 7 years ago • 9 comments

I want to send Message Attachments to mattermost server from hubot i tried with res.send() and res.reply() but it's not working. https://docs.mattermost.com/developer/message-attachments.html?highlight=attachment

how can i send it?

akki005 avatar Aug 04 '17 13:08 akki005

@akki005 Did you get it to work? Would like to use attachments, too

meilon avatar Apr 04 '18 15:04 meilon

Any idea? I have the same problem.

Fabianski-tr avatar Jun 07 '18 16:06 Fabianski-tr

I got it to work:

module.exports = (robot) ->
    robot.hear /myTriggerWord/i, (res) ->
        # Creating attachments
        attachment1 = {
            "fallback": "test",
            ...
        }

        attachments = [attachment1, attachment2, ...]

        # Create the message for mattermost
        msg = {}
        # Room is important, but in an .hear you know it already
        msg.room = res.envelope.room
        msg.attachments = attachments

        # Use robot.emit, not res.send or res.reply
        robot.emit 'slack.attachment', msg

Hope that's enough.

meilon avatar Jun 08 '18 07:06 meilon

Could you share a full example? I'm blocked, I've done a case following your example but it doesn't allow me to run Hubot.

Thank you for your answer (I'm desperate).

Fabianski-tr avatar Jun 08 '18 11:06 Fabianski-tr

Sorry, this is a full example. Of course I didn't write out the full attachment stuff, but my example should get you going

meilon avatar Jun 11 '18 08:06 meilon

Yes, Thank you very much @meilon

I was able to create an attachment with buttons following your example and the documentation of the Mattermost API.

My problem is that I don't know how to do it so that the buttons can interact with Hubot when it comes to being clicked by the user.

Fabianski-tr avatar Jun 11 '18 10:06 Fabianski-tr

I don’t think that this would be possible, hubot is a chat bot, so the user has to say something for hubot to get new input

meilon avatar Jun 13 '18 16:06 meilon

Yes, that's exactly what I want you to do.

When the user clicks on the button, execute the command that he needs hubot.

For example:

module.exports = (robot) ->
    robot.hear /bot (start|hello)/i, (res) ->
        attachment0 = {
            "author_name": "Bot",
            "title": "Welcome",
            "text": "Welcome a Test Bot. ¿What can I do for you? ",
            "callback_id": "welcome",
            "color": "#65c78e",
            "actions": [
                {
                    "name": "Test 1",
                    "integration": {
                        "url": "http://127.0.0.1:8080/hubot/incoming",
                        "context": {
                            "action": "test1"
                            }
                    }
                },
                {
                    "name": "Test 2",
                    "integration": {
                        "url": "http://127.0.0.1:8080/hubot/incoming",
                        "context": {
                            "action": "test2"
                            }
                    }
                }
            ]
        }
        attachments = [attachment0]
        msg = {}
        msg.room = res.envelope.room
        msg.attachments = attachments
        robot.emit 'slack.attachment', msg

When the user clicks on the "Test 1" button, what I want is for him to post in Mattermost "Bot Test1" and for Hubot to respond to this new command.

The problem is that I don't know how to make that post to the chat through the button and hubot.

Fabianski-tr avatar Jun 13 '18 17:06 Fabianski-tr

Hi there, robot.emit 'slack.attachment', msg did not work for me. However I figured out that res.send data at the end works perfectly.

Full example:

robot.respond /sometext to trigger (.*)/i, (res) ->
  data = {
    "message": "msg",
    "props": {
      "attachments": [
        {
          "pretext": "This is the attachment pretext.",
          "text": "This is the attachment text.",
          "actions": [
            {
              "name": "Ephemeral Message",
              "integration": {
                "url": "http://127.0.0.1:7357",
                "context": {
                  "action": "do_something_ephemeral"
                }
              }
            }, {
              "name": "Update",
              "integration": {
                "url": "http://127.0.0.1:7357",
                "context": {
                  "action": "do_something_update"
                }
              }
            }
          ]
        }
      ]
    }
  }
  res.send data

image

Vicleslie avatar Nov 07 '19 17:11 Vicleslie