Skip to content

Commit 5d63f77

Browse files
authored
Merge pull request #7 from codeccoop/feat/slack
Slack addon
2 parents 89d68d7 + 716a532 commit 5d63f77

File tree

13 files changed

+687
-14
lines changed

13 files changed

+687
-14
lines changed

forms-bridge/addons/gsheets/hooks.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,7 @@ function ( $defaults, $addon, $schema ) {
8787
'name' => 'scope',
8888
'label' => __( 'Scope', 'forms-bridge' ),
8989
'type' => 'text',
90-
'value' =>
91-
'https://www.googleapis.com/auth/drive.readonly https://www.googleapis.com/auth/spreadsheets',
90+
'value' => 'https://www.googleapis.com/auth/drive.readonly https://www.googleapis.com/auth/spreadsheets',
9291
'required' => true,
9392
),
9493
array(
@@ -147,8 +146,7 @@ function ( $defaults, $addon, $schema ) {
147146
'name' => '',
148147
'schema' => 'Bearer',
149148
'oauth_url' => 'https://accounts.google.com/o/oauth2/v2',
150-
'scope' =>
151-
'https://www.googleapis.com/auth/drive.readonly https://www.googleapis.com/auth/spreadsheets',
149+
'scope' => 'https://www.googleapis.com/auth/drive.readonly https://www.googleapis.com/auth/spreadsheets',
152150
'client_id' => '',
153151
'client_secret' => '',
154152
'access_token' => '',
@@ -173,8 +171,7 @@ function ( $data, $template_id ) {
173171
return $data;
174172
}
175173

176-
$data['bridge']['endpoint'] =
177-
'/v4/spreadsheets/' . $data['bridge']['endpoint'];
174+
$data['bridge']['endpoint'] = '/v4/spreadsheets/' . $data['bridge']['endpoint'];
178175
return $data;
179176
},
180177
10,
@@ -184,7 +181,7 @@ function ( $data, $template_id ) {
184181
add_filter(
185182
'http_bridge_oauth_url',
186183
function ( $url, $verb ) {
187-
if ( strpos( $url, 'accounts.google.com' ) === false ) {
184+
if ( false === strpos( $url, 'accounts.google.com' ) ) {
188185
return $url;
189186
}
190187

16.6 KB
Loading
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
/**
3+
* Class Slack_Addon
4+
*
5+
* @package formsbridge
6+
*/
7+
8+
namespace FORMS_BRIDGE;
9+
10+
if ( ! defined( 'ABSPATH' ) ) {
11+
exit();
12+
}
13+
14+
require_once 'class-slack-form-bridge.php';
15+
require_once 'hooks.php';
16+
17+
/**
18+
* Slack addon class.
19+
*/
20+
class Slack_Addon extends Addon {
21+
/**
22+
* Holds the addon's title.
23+
*
24+
* @var string
25+
*/
26+
public const TITLE = 'Slack';
27+
28+
/**
29+
* Holds the addon's name.
30+
*
31+
* @var string
32+
*/
33+
public const NAME = 'slack';
34+
35+
/**
36+
* Holds the addon's custom bridge class.
37+
*
38+
* @var string
39+
*/
40+
public const BRIDGE = '\FORMS_BRIDGE\Slack_Form_Bridge';
41+
42+
/**
43+
* Performs a request against the backend to check the connexion status.
44+
*
45+
* @param string $backend Backend name.
46+
*
47+
* @return boolean
48+
*/
49+
public function ping( $backend ) {
50+
$bridge = new Slack_Form_Bridge(
51+
array(
52+
'name' => '__slack-' . time(),
53+
'endpoint' => '/api/conversations.list',
54+
'method' => 'GET',
55+
'backend' => $backend,
56+
)
57+
);
58+
59+
$response = $bridge->submit();
60+
61+
if ( is_wp_error( $response ) ) {
62+
Logger::log( 'Slack backend ping error response', Logger::ERROR );
63+
Logger::log( $response, Logger::ERROR );
64+
return false;
65+
}
66+
67+
return true;
68+
}
69+
70+
/**
71+
* Performs a GET request against the backend endpoint and retrive the response data.
72+
*
73+
* @param string $endpoint API endpoint.
74+
* @param string $backend Backend name.
75+
*
76+
* @return array|WP_Error
77+
*/
78+
public function fetch( $endpoint, $backend ) {
79+
$bridge = new Slack_Form_Bridge(
80+
array(
81+
'name' => '__slack-' . time(),
82+
'endpoint' => $endpoint,
83+
'method' => 'GET',
84+
'backend' => $backend,
85+
),
86+
'zulip'
87+
);
88+
89+
return $bridge->submit();
90+
}
91+
}
92+
93+
Slack_Addon::setup();
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
<?php
2+
/**
3+
* Class Slack_Form_Bridge
4+
*
5+
* @package formsbridge
6+
*/
7+
8+
namespace FORMS_BRIDGE;
9+
10+
use FBAPI;
11+
use WP_Error;
12+
13+
if ( ! defined( 'ABSPATH' ) ) {
14+
exit();
15+
}
16+
17+
/**
18+
* Form bridge implementation for the Slack API.
19+
*/
20+
class Slack_Form_Bridge extends Form_Bridge {
21+
/**
22+
* Holdes the current HTTP request.
23+
*
24+
* @var array|null
25+
*/
26+
private static $request;
27+
28+
/**
29+
* Submits payload and attachments to the bridge's backend.
30+
*
31+
* @param array $payload Payload data.
32+
* @param array $attachments Submission's attached files.
33+
*
34+
* @return array|WP_Error Http request response.
35+
*/
36+
public function submit( $payload = array(), $attachments = array() ) {
37+
$uploads = FBAPI::get_uploads();
38+
39+
if ( ! empty( $uploads ) ) {
40+
$attachments = Forms_Bridge::attachments( $uploads );
41+
$backend = $this->backend();
42+
43+
foreach ( $attachments as $name => $path ) {
44+
$info = pathinfo( $path );
45+
$filename = $info['basename'];
46+
47+
$response = $backend->post(
48+
'/api/files.getUploadURLExternal',
49+
array(
50+
'length' => filesize( $path ),
51+
'filename' => $filename,
52+
),
53+
array(
54+
'Content-Type' => 'application/x-www-form-urlencoded',
55+
)
56+
);
57+
58+
if ( is_wp_error( $response ) ) {
59+
return $response;
60+
}
61+
62+
if ( isset( $response['data']['error'] ) ) {
63+
return new WP_Error( 'slack_upload', __( 'Can not upload a file to Slack', 'forms-bridge' ), $response['data'] );
64+
}
65+
66+
$file_id = $response['data']['file_id'];
67+
$upload_url = $response['data']['upload_url'];
68+
69+
$response = http_bridge_post(
70+
$upload_url,
71+
file_get_contents( $path ),
72+
array(
73+
'Content-Type' => 'application/octet-stream',
74+
),
75+
);
76+
77+
if ( is_wp_error( $response ) ) {
78+
return $response;
79+
}
80+
81+
$attachments[ $name ] = $file_id;
82+
}
83+
84+
$files = array();
85+
foreach ( $attachments as $name => $file_id ) {
86+
$files[] = array(
87+
'id' => $file_id,
88+
'title' => $name,
89+
);
90+
}
91+
92+
$response = $backend->post(
93+
'/api/files.completeUploadExternal',
94+
array(
95+
'files' => wp_json_encode( $files ),
96+
'channel_id' => $payload['channel'] ?? $payload['channel_id'] ?? null,
97+
),
98+
array(
99+
'Content-Type' => 'application/x-www-form-urlencoded',
100+
),
101+
);
102+
103+
if ( is_wp_error( $response ) ) {
104+
return $response;
105+
}
106+
107+
if ( isset( $response['data']['error'] ) ) {
108+
return new WP_Error( 'slack_upload', __( 'Can not upload a file to Slack', 'forms-bridge' ), $response['data'] );
109+
}
110+
111+
$annex = "\n\n----\n" . esc_html( __( 'Attachments', 'forms-bridge' ) ) . ":\n";
112+
113+
foreach ( $response['data']['files'] as $upload ) {
114+
$annex .= "* [{$upload['name']}]({$upload['permalink']})\n";
115+
}
116+
117+
if ( isset( $payload['markdown_text'] ) ) {
118+
$payload['markdown_text'] .= $annex;
119+
} else {
120+
$payload['text'] .= $annex;
121+
}
122+
}
123+
124+
add_filter(
125+
'http_bridge_request',
126+
static function ( $request ) {
127+
self::$request = $request;
128+
return $request;
129+
},
130+
10,
131+
1
132+
);
133+
134+
$response = parent::submit( $payload, array() );
135+
136+
if ( is_wp_error( $response ) ) {
137+
return $response;
138+
}
139+
140+
if ( isset( $response['data']['error'] ) ) {
141+
return new WP_Error(
142+
'slack_rpc_error',
143+
'Slack bridge response error',
144+
array(
145+
'request' => self::$request,
146+
'response' => $response,
147+
)
148+
);
149+
}
150+
151+
return $response;
152+
}
153+
}

0 commit comments

Comments
 (0)