Closed tplessis closed 8 years ago
@tplessis Sorry for the delayed response.
The problem in your example is that you're using the AttachmentBuilder
directly, and sending just the attachment to Slack. You need to add the attachment to a Message
object.
Here are two examples that will work:
addAttachment
to a Message
object:I modified your example slightly, using variable names that more closely match the type of object being created. At the end of the example, you'll see I created a Message
object, and added the Attachment
to it before sending the message to Slack.
$attachmentBuilder = $this->phlack->getAttachmentBuilder();
$attachmentBuilder
->setTitle($title)
->setTitleLink('http://www.totemnumerique.com')
->setPretext($pretext)
->setText($body)
->setColor($color)
->addField('Project', 'Vimaedis API', true)
->setFallback($title . ' ' . $pretext);
$attachment = $attachmentBuilder->create();
$message = new \Crummy\Phlack\Message\Message('This message contains an attachment.');
$message->addAttachment($attachment);
$this->phlack->send($message);
MessageBuilder
:You can also use the MessageBuilder
directly to create the Message
object and add the Attachment. The MessageBuilder
supports method chaining to allow for adding multiple Attachment
objects to a single message. Note: You must call end()
once for each attachment so that the MessageBuilder
knows to create the Attachment
object and return the MessageBuilder
object for further modification.
$messageBuilder = $this->phlack->getMessageBuilder(); # Get the MessageBuilder
$messageBuilder
->setText('This message contains multiple attachments.') # Message text.
->createAttachment() # Returns the AttachmentBuilder.
->setTitle($title)
->setTitleLink('http://www.totemnumerique.com')
->setPretext($pretext)
->setText($body)
->setColor($color)
->addField('Project', 'Vimaedis API', true)
->setFallback($title . ' ' . $pretext)
->end() # Creates the first attachment and returns the MessageBuilder
->setUsername('tplessis') # Sets username on the Message object.
->createAttachment() # Returns the AttachmentBuilder.
->setTitle('Attachment #2')
->setFallback('Attachment #2 for example purposes')
->setText('Add multiple attachments to a Phlack Message via method chaining.')
->end() # Creates the second attachment and returns the MessageBuilder.
;
$message = $messageBuilder->create(); # Builds the Message containing the Attachment objects.
$this->phlack->send($message);
Hope that helps! If you're still having trouble, please let me know.
Hi mcrumm,
Thx a lot for your support, it works perfectly now with the first solution! Could you update the doc ?
Regards,
Yes, I'll add similar examples to the docs to make this clearer.
I've added more docs to the README, citing these examples for creating messages. Thanks, again, for the feedback!
Hello guys,
When i'm sending an attachment, like this :
Text is sending twice in the slack message. One time on the top of the message (as the pretext ?) and one time in the attachment. In addition, title and link where not displayed.
Regards, Thomas