Skip to content

Instantly share code, notes, and snippets.

@stojg
Last active January 24, 2018 21:31
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save stojg/4975086 to your computer and use it in GitHub Desktop.
Save stojg/4975086 to your computer and use it in GitHub Desktop.
<?php
/**
* Send an message to a Flowdock "flow".
*
* # Initialization:
* <code>
* $fd = new Flowdock();
* $fd->setToken('asdadsads');
* </code>
*
* or
*
* <code>
* $fd = new Flowdock();
* </code>
*
* # Usage:
*
* ## Send to chat:
*
* <code>
* $res = $fd->pushToChat('Message', 'external_username', array('test','tag'));
* </code>
*
* ## Send to inbox
*
* <code>
* $res = $fd->pushToInbox('New test', 'test@silverstripe.com', 'Subject', 'This is a test message');
* </code>
*/
class Flowdock {
/**
* @var string
*/
protected $token = '';
/**
* @param string $token - the flowdock api key for a channel
*
*/
public function __construct($token='') {
$this->token = $token;
}
/**
* Get the currently set API token
*
* @return string
*/
public function getToken() {
return $this->token;
}
/**
* Set the API token
*
* @return Flowdock
*/
public function setToken($token) {
$this->token = $token;
return $this;
}
/**
* Sends a message to the chat
*
* @param string $content
* @param string $externalUserName
* @param array $tags
* @return string - the result, should be '{}'
*/
public function pushToChat($content, $externalUserName, $tags = array()) {
$data = array(
'content' => $content,
'external_user_name' => $externalUserName,
'tags' => implode(',', $tags)
);
return $this->push('https://api.flowdock.com/v1/messages/chat/', $data);
}
/**
* Sends a message to the inbox
*
* Required:
* @param $source - Human readable identifier of the application that uses the Flowdock API. Only alphanumeric characters,
* underscores and whitespace can be used. This identifier will be used as the primary method of
* categorization for the messages. Example value: Awesome Issue Management App
* @param $from_address - Email address of the message sender. The email address is used to show a avatar of the
* sender. You can customize the avatar by registering the address in Gravatar.
* Example value: john.doe@yourdomain.com
* @param $subject - Subject line of the message, will be displayed as the title of Team Inbox message.
* @param $content - Content of the message, will be displayed as the body of Team Inbox message.
*
* Optional:
* @param $from_name - Name of the message sender. Example value: John Doe
* @param $reply_to - Email address for replies, will be used when replying to the message from Flowdock.
* @param $project - Human readable identifier for more detailed message categorization. Only alphanumeric characters,
* underscores and whitespace can be used. This identifier will be used as the secondary method of
* categorization for the messages. Example value: My Project
* @param $tags - Tags of the message, separated by commas. Example value: cool,stuff
* @param $link - Link associated with the message. This will be used to link the message subject in Team Inbox.
* Example value: http://www.flowdock.com/
*/
public function pushToInbox($source, $from_address, $subject, $content, $from_name=null, $reply_to = null,
$project = null,
$tags = array(), $link=null) {
$data = array(
'source' => $source,
'from_address' => $from_address,
'subject' => $subject,
'content' => $content,
'format' => 'html'
);
if($from_name) {
$data['from_name'] = $from_name;
}
if($reply_to) {
$data['reply_to'] = $reply_to;
}
if($project) {
$data['project'] = $project;
}
if($tags) {
$data['tags'] = $tags;
}
if($link) {
$data['link'] = $link;
}
return $this->push('https://api.flowdock.com/v1/messages/team_inbox/', $data);
}
/**
* Do a curl request to Flowdock
*
* @param string $url - the REST API endpoint url
* @param array $data
*/
protected function push($url, $data) {
$ch = curl_init($url.$this->token);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$result = curl_exec($ch);
if(curl_error($ch)) {
echo(curl_error($ch)).PHP_EOL;
exit(1);
}
$info = curl_getinfo($ch);
curl_close($ch);
return $result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment