-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.php
More file actions
153 lines (113 loc) · 3.79 KB
/
plugin.php
File metadata and controls
153 lines (113 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
class toggl extends SlackServicePlugin {
public $name = 'Toggl Service';
public $desc = "Log your day's time via Slack commands.";
public $cfg = array('has_token' => true);
private $toggl_url = 'https://www.toggl.com/api/v8/';
private $toggl_userpass = '{api_token}:api_token';
private $channel = '#general';
private $botname = 'togglbot';
private $commands = array('create', 'start', 'stop');
function onView () {
return $this->smarty->fetch('view.txt');
}
function onHook ($req) {
$this->channel = $req['post']['channel_id'];
try {
$full_command = $this->removeTrigger($req['post']['text'], $req['post']['trigger_word']);
list($command, $args) = $this->parseCommand($full_command);
if (in_array($command, $this->commands))
call_user_func(array($this, "{$command}TimeEntry"), $args);
else
$this->postError("Command \"{$command}\" not recognized.");
} catch (Exception $e) {
$this->postError($e->getMessage());
}
}
private function removeTrigger ($input, $trigger) {
return trim(substr($input, strlen($trigger)));
}
private function parseCommand ($input) {
if (empty($input))
throw new Exception('No command specified.');
// 2 pieces: command and arguments
$words = explode(' ', $input, 2);
return array($words[0], $words[1]);
}
private function postError ($message) {
$this->postToChannel('Error: ' . $message, array(
'channel' => $this->channel,
'username' => $this->botname
));
}
private function postSuccess ($message = 'Success!') {
$this->postToChannel($message, array(
'channel' => $this->channel,
'username' => $this->botname
));
}
private function parseCreateArgs ($args) {
if (empty($args))
throw new Exception('Duration must be supplied.');
// First "word" is duration; remainder is description
$args = explode(' ', $args, 2);
if (!ctype_digit ($args[0]))
throw new Exception('Duration must be an integer.');
return $args;
}
private function createTimeEntry ($args) {
list($duration, $description) = $this->parseCreateArgs($args);
$this->sendRequest('time_entries', array(
'time_entry' => array(
'description' => $description,
'duration' => $duration,
'start' => date('c'), // ISO 8601
'created_with' => 'Slack'
)
));
$this->postSuccess('Time entry created successfully.');
}
private function startTimeEntry ($args) {
$results = $this->sendRequest('time_entries/start', array(
'time_entry' => array(
'description' => $args,
'created_with' => 'Slack'
)
));
$id = json_decode($results)->data->id;
$this->postSuccess('Time entry started succesfully. ID: ' . $id);
}
private function stopTimeEntry ($args) {
if (empty($args))
throw new Exception('Time entry ID not provided.');
$this->sendRequest("time_entries/{$args}/stop", array(), 'PUT');
$this->postSuccess("Time entry {$args} stopped succesfully.");
}
private function sendRequest ($request_url, $params = array(), $method = 'POST') {
$data = json_encode($params);
$headers = array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data),
'Authorization: Basic ' . base64_encode($this->toggl_userpass)
);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $this->toggl_url . $request_url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_USERPWD => $this->toggl_userpass,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_CAINFO => __DIR__ . '/cacert.pem', // Bundle of CA Root Certificates
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_POSTFIELDS => $data
));
$results = curl_exec($curl);
$info = curl_getinfo($curl);
//$error = curl_error($curl);
curl_close($curl);
// There was an error
if ($info['http_code'] != 200)
throw new Exception('Request not successful.');
return $results;
}
}