mirror of
https://we.phorge.it/source/phorge.git
synced 2024-12-23 22:10:55 +01:00
Repair Flowdock adapter for ChatBot
Summary: Restores functionality for Flowdock->Chatbot adapter. Most likely the result of API changes in the year since the original patch was contributed, the flowdock adapter no longer worked. This makes a few tweaks to both the base streaming adapter class and the flowdock adpater. I took care to not disturb the functionality of the campfire adapter, but I don't have any way to test it. Test Plan: I am new here and I have no idea what to write other than sarcastic things but I'll most like amend this after review. Reviewers: epriestley, #blessed_reviewers Reviewed By: epriestley, #blessed_reviewers Subscribers: epriestley, Korvin Differential Revision: https://secure.phabricator.com/D10168
This commit is contained in:
parent
50a393913c
commit
5b4fb3b155
2 changed files with 65 additions and 48 deletions
|
@ -3,11 +3,12 @@
|
|||
abstract class PhabricatorBotBaseStreamingProtocolAdapter
|
||||
extends PhabricatorBaseProtocolAdapter {
|
||||
|
||||
protected $readHandles;
|
||||
protected $multiHandle;
|
||||
protected $authtoken;
|
||||
|
||||
private $readBuffers;
|
||||
private $authtoken;
|
||||
private $server;
|
||||
private $readHandles;
|
||||
private $multiHandle;
|
||||
private $active;
|
||||
private $inRooms = array();
|
||||
|
||||
|
@ -38,26 +39,28 @@ abstract class PhabricatorBotBaseStreamingProtocolAdapter
|
|||
|
||||
// Set up the curl stream for reading
|
||||
$url = $this->buildStreamingUrl($room_id);
|
||||
$this->readHandle[$url] = curl_init();
|
||||
curl_setopt($this->readHandle[$url], CURLOPT_URL, $url);
|
||||
curl_setopt($this->readHandle[$url], CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($this->readHandle[$url], CURLOPT_FOLLOWLOCATION, 1);
|
||||
$ch = $this->readHandles[$url] = curl_init();
|
||||
|
||||
curl_setopt($ch, CURLOPT_URL, $url);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
|
||||
curl_setopt(
|
||||
$this->readHandle[$url],
|
||||
$ch,
|
||||
CURLOPT_USERPWD,
|
||||
$this->authtoken.':x');
|
||||
|
||||
curl_setopt(
|
||||
$this->readHandle[$url],
|
||||
$ch,
|
||||
CURLOPT_HTTPHEADER,
|
||||
array('Content-type: application/json'));
|
||||
curl_setopt(
|
||||
$this->readHandle[$url],
|
||||
$ch,
|
||||
CURLOPT_WRITEFUNCTION,
|
||||
array($this, 'read'));
|
||||
curl_setopt($this->readHandle[$url], CURLOPT_BUFFERSIZE, 128);
|
||||
curl_setopt($this->readHandle[$url], CURLOPT_TIMEOUT, 0);
|
||||
curl_setopt($ch, CURLOPT_BUFFERSIZE, 128);
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, 0);
|
||||
|
||||
curl_multi_add_handle($this->multiHandle, $this->readHandle[$url]);
|
||||
curl_multi_add_handle($this->multiHandle, $ch);
|
||||
|
||||
// Initialize read buffer
|
||||
$this->readBuffers[$url] = '';
|
||||
|
@ -153,10 +156,15 @@ abstract class PhabricatorBotBaseStreamingProtocolAdapter
|
|||
}
|
||||
|
||||
protected function getAuthorizationHeader() {
|
||||
return 'Basic '.base64_encode($this->authtoken.':x');
|
||||
return 'Basic '.$this->getEncodedAuthToken();
|
||||
}
|
||||
|
||||
protected function getEncodedAuthToken() {
|
||||
return base64_encode($this->authtoken.':x');
|
||||
}
|
||||
|
||||
abstract protected function buildStreamingUrl($channel);
|
||||
|
||||
abstract protected function processMessage($raw_object);
|
||||
|
||||
}
|
||||
|
|
|
@ -8,42 +8,50 @@ final class PhabricatorBotFlowdockProtocolAdapter
|
|||
}
|
||||
|
||||
protected function buildStreamingUrl($channel) {
|
||||
$organization = $this->getConfig('organization');
|
||||
$organization = $this->getConfig('flowdock.organization');
|
||||
if (empty($organization)) {
|
||||
$this->getConfig('organization');
|
||||
}
|
||||
if (empty($organization)) {
|
||||
throw new Exception(
|
||||
'"flowdock.organization" configuration variable not set');
|
||||
}
|
||||
|
||||
|
||||
$ssl = $this->getConfig('ssl');
|
||||
|
||||
$url = ($ssl) ? 'https://' : 'http://';
|
||||
$url .= "stream.flowdock.com/flows/{$organization}/{$channel}";
|
||||
|
||||
$url .= "{$this->authtoken}@stream.flowdock.com/flows/{$organization}/{$channel}";
|
||||
return $url;
|
||||
}
|
||||
|
||||
protected function processMessage($m_obj) {
|
||||
$command = null;
|
||||
switch ($m_obj['event']) {
|
||||
case 'message':
|
||||
$command = 'MESSAGE';
|
||||
break;
|
||||
default:
|
||||
// For now, ignore anything which we don't otherwise know about.
|
||||
break;
|
||||
}
|
||||
$command = null;
|
||||
switch ($m_obj['event']) {
|
||||
case 'message':
|
||||
$command = 'MESSAGE';
|
||||
break;
|
||||
default:
|
||||
// For now, ignore anything which we don't otherwise know about.
|
||||
break;
|
||||
}
|
||||
|
||||
if ($command === null) {
|
||||
return false;
|
||||
}
|
||||
if ($command === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// TODO: These should be usernames, not user IDs.
|
||||
$sender = id(new PhabricatorBotUser())
|
||||
->setName($m_obj['user']);
|
||||
// TODO: These should be usernames, not user IDs.
|
||||
$sender = id(new PhabricatorBotUser())
|
||||
->setName($m_obj['user']);
|
||||
|
||||
$target = id(new PhabricatorBotChannel())
|
||||
->setName($m_obj['flow']);
|
||||
$target = id(new PhabricatorBotChannel())
|
||||
->setName($m_obj['flow']);
|
||||
|
||||
return id(new PhabricatorBotMessage())
|
||||
->setCommand($command)
|
||||
->setSender($sender)
|
||||
->setTarget($target)
|
||||
->setBody($m_obj['content']);
|
||||
return id(new PhabricatorBotMessage())
|
||||
->setCommand($command)
|
||||
->setSender($sender)
|
||||
->setTarget($target)
|
||||
->setBody($m_obj['content']);
|
||||
}
|
||||
|
||||
public function writeMessage(PhabricatorBotMessage $message) {
|
||||
|
@ -59,15 +67,16 @@ final class PhabricatorBotFlowdockProtocolAdapter
|
|||
private function speak(
|
||||
$body,
|
||||
PhabricatorBotTarget $flow) {
|
||||
|
||||
list($organization, $room_id) = explode(':', $flow->getName());
|
||||
|
||||
$this->performPost(
|
||||
"/flows/{$organization}/{$room_id}/messages",
|
||||
array(
|
||||
'event' => 'message',
|
||||
'content' => $body));
|
||||
}
|
||||
// The $flow->getName() returns the flow's UUID,
|
||||
// as such, the Flowdock API does not require the organization
|
||||
// to be specified in the URI
|
||||
$this->performPost(
|
||||
'/messages',
|
||||
array(
|
||||
'flow' => $flow->getName(),
|
||||
'event' => 'message',
|
||||
'content' => $body));
|
||||
}
|
||||
|
||||
public function __destruct() {
|
||||
if ($this->readHandles) {
|
||||
|
|
Loading…
Reference in a new issue