Closed marcuslindblom closed 8 years ago
Take a look at attachments: https://api.slack.com/docs/attachments
@errodr Ok, but in my coffee script, how do I compose a message after msg.send
?
module.exports = (robot) ->
robot.respond /team (.*)$/i, (msg)->
team = msg.match[1]
query msg, "http://api.xxx.xx/#{team}"
query = (msg, url) ->
msg.http(url)
.get() (err, res, body) ->
data = JSON.parse(body)
if err
msg.send "Fail! Something went wrong. Couldn't start the build for some reason"
else
if data
msg.send ""
else
msg.send "Fail..."
Here is an example of what I used to send the attachment:
sendMessage = (results, msg) ->
robot.emit 'slack-attachment',
message:
room: msg.message.room
content: results
You can see that a slack-attachment is emitted. The content (results) of that message is of the form:
{
"fallback": "New ticket from Andrea Lee - Ticket #1943: Can't rest my password - https://groove.hq/path/to/ticket/1943",
"pretext": "New ticket from Andrea Lee",
"title": "Ticket #1943: Can't reset my password",
"title_link": "https://groove.hq/path/to/ticket/1943",
"text": "Help! I tried to reset my password but nothing happened!",
"color": "#7CD197"
}
@errodr emit
does not send anything to slack. How can I use msg.send and format the message?
@marcuslindblom were you able to find a solution for this?
@yusufhm no, sorry, and I couldn't find any working example.
@marcuslindblom I've been able to do it using the customMessage() method from the adapter (lines 254-281 in https://github.com/slackhq/hubot-slack/blob/master/src/slack.coffee). Here's how I used it:
# create the message with attachment object
msgData = {
channel: res.message.room
text: "Latest changes"
attachments: [
{
fallback: "Comparing #{latestRelease.name}...#{latestRelease.target_commitish} - #{compare.html_url}",
title: "Comparing #{latestRelease.name}...#{latestRelease.target_commitish}"
title_link: compare.html_url
text: commits_summary
mrkdwn_in: ["text"]
}
]
}
# post the message
robot.adapter.customMessage msgData
Look at both the method code and the Attachment documentation somebody posted earlier and you should be able to do what you want.
This is a duplicate of https://github.com/slackhq/hubot-slack/issues/43 . I just posted a work around there, but basically it's like @yusufhm posted, you can send a customMessage to do it.
This is being resolved in v4. Closing for the time being.
@DEGoodmanWilson When v4 will be released?
@CatTail v4 is out! @DEGoodmanWilson How do we send attachments in v4? Cannot find example easily. Thanks!
@spacediver It is easy: Just add fields to your message object, per the docs: https://api.slack.com/docs/message-attachments
@DEGoodmanWilson Should I do msg.send
or robot.emit
? I cannot google up useful (and v3-friendly) examples. If i do
msg.send({
attachments: [{
title: 'title',
fallback: 'title',
title_link: 'http://example.com',
}],
username: process.env.HUBOT_SLACK_BOTNAME,
});
It ends up displayed in rich, but just like if as_user
was set to false. Adding as_user: true
into object does not help. Calls like msg.send('plain text');
displayed correctly (as if as_user=true
, with icon and bot user link).
How to do that right?
Thanks.
Where are you adding as_user
into the object? Just to verify?
like this:
msg.send({
attachments: [{
title: 'a title',
fallback: 'a fallback'
title_link: 'http://example.com',
}],
username: process.env.HUBOT_SLACK_BOTNAME,
as_user: true,
});
(sibling to attachments
, parallel to documentation).
OK, that's a bug! Just wanted to verify. Let's take this to a new issue?
Sure, thanks! Should be tested on xoxb-
bot token.
I'm trying to pass attachment using chat api like this:
attachment = {
attachments: [{
title: 'a title'
fallback: 'a fallback'
title_link: 'http://example.com'
}]
}
robot.adapter.client.web.chat.postMessage(msg.message.room, {attachment}, {as_user: true, unfurl_links: false})
Tried this too:
attachment2 = {attachments:[
text: "Message content"
title: "message title"
image_url: "https://www.snipstock.com/assets/cdn/png/new/b50c79a7995782cedc741493b3c13e6b.png"
]}
robot.send attachment2
But it fails with invalid_array_arg
nhandled rejection SlackAPIError: invalid_array_arg
at makeAPICallPromiseResolverInner (/home/ubuntu/pooja/scripts/myhubot/node_modules/@slack/client/lib/clients/client.js:156:22)
at handleHttpResponse (/home/ubuntu/pooja/scripts/myhubot/node_modules/@slack/client/lib/clients/transports/call-transport.js:105:5)
at handleTransportResponse (/home/ubuntu/pooja/scripts/myhubot/node_modules/@slack/client/lib/clients/transports/call-transport.js:142:19)
at apply (/home/ubuntu/pooja/scripts/myhubot/node_modules/@slack/client/node_modules/lodash/lodash.js:483:17)
at wrapper (/home/ubuntu/pooja/scripts/myhubot/node_modules/@slack/client/node_modules/lodash/lodash.js:5293:16)
at Request.handleRequestTranportRes (/home/ubuntu/pooja/scripts/myhubot/node_modules/@slack/client/lib/clients/transports/request.js:20:5)
at apply (/home/ubuntu/pooja/scripts/myhubot/node_modules/@slack/client/node_modules/lodash/lodash.js:483:17)
at Request.wrapper [as _callback] (/home/ubuntu/pooja/scripts/myhubot/node_modules/@slack/client/node_modules/lodash/lodash.js:5293:16)
at Request.self.callback (/home/ubuntu/pooja/scripts/myhubot/node_modules/request/request.js:187:22)
at emitTwo (events.js:106:13)
at Request.emit (events.js:191:7)
at Request.<anonymous> (/home/ubuntu/pooja/scripts/myhubot/node_modules/request/request.js:1048:10)
at emitOne (events.js:96:13)
at Request.emit (events.js:188:7)
Am I missing something in syntax?
@p00j4 that method takes the following arguments: channel_id
, text
, options
. so your code should look something like this:
robot.adapter.client.web.chat.postMessage(msg.message.room, "message content", {
as_user: true,
unfurl_links: false,
attachments: [
{
title: 'a title',
fallback: 'a fallback',
title_link: 'http://example.com'
}
]
})
ah! thanks @aoberoi the bug was with me sending "as_user" etc in different argument. Thanks much for the quick help 👍
as_user=true is not sending DM as user its still sending as bot. Please help.
How can I format messages from my hubot scripts? This message:
msg.send "Foo <@U123> bar <http://slack.com>"
generatesFoo @U123 bar <http://slack.com>
when my hubot prints it to a channel? Are there any script example using more advanced formatting?