2020use Psr \Http \Message \ResponseInterface ;
2121use GuzzleHttp \Psr7 \Response ;
2222
23- require_once './vendor/autoload.php ' ;
23+ use Google_Service_Kgsearch ;
24+ use Google_Service_Kgsearch_SearchResponse ;
25+
2426// [END functions_slack_setup]
2527
2628// [START functions_verify_webhook]
2729/**
28- * Verify that the webhook request came from Slack.
29- */
30+ * Verify that the webhook request came from Slack.
31+ */
3032function isValidSlackWebhook (ServerRequestInterface $ request ): bool
3133{
3234 /**
33- * PHP Functions Framework does not support "global"/instance-scoped
34- * constants, so we fetch these values within PHP functtions themselves.
35- */
36- $ SLACK_SECRET = getenv (" SLACK_SECRET " );
35+ * PHP Functions Framework does not support "global"/instance-scoped
36+ * constants, so we fetch these values within PHP functtions themselves.
37+ */
38+ $ SLACK_SECRET = getenv (' SLACK_SECRET ' );
3739
3840 // Check for headers
39- $ timestamp = $ request ->getHeader (" X-Slack-Request-Timestamp " );
40- $ signature = $ request ->getHeader (" X-Slack-Signature " );
41+ $ timestamp = $ request ->getHeader (' X-Slack-Request-Timestamp ' );
42+ $ signature = $ request ->getHeader (' X-Slack-Signature ' );
4143 if (!$ timestamp || !$ signature ) {
4244 return false ;
4345 } else {
@@ -55,63 +57,63 @@ function isValidSlackWebhook(ServerRequestInterface $request): bool
5557
5658// [START functions_slack_format]
5759/**
58- * Format the Knowledge Graph API response into a richly formatted Slack message.
59- */
60+ * Format the Knowledge Graph API response into a richly formatted Slack message.
61+ */
6062function formatSlackMessage ($ kgResponse , $ query ): string
6163{
6264 $ responseJson = [
63- " response_type " => " in_channel " ,
64- " text " => " Query: " . $ query
65+ ' response_type ' => ' in_channel ' ,
66+ ' text ' => ' Query: ' . $ query
6567 ];
6668
67- $ entityList = $ kgResponse [" itemListElement " ];
69+ $ entityList = $ kgResponse [' itemListElement ' ];
6870
6971 // Extract the first entity from the result list, if any
7072 if (empty ($ entityList )) {
71- $ attachmentJson = [" text " => " No results match your query... " ];
72- $ responseJson [" attachments " ] = $ attachmentJson ;
73+ $ attachmentJson = [' text ' => ' No results match your query... ' ];
74+ $ responseJson [' attachments ' ] = $ attachmentJson ;
7375
7476 return json_encode ($ responseJson );
7577 }
7678
77- $ entity = $ entityList [0 ][" result " ];
79+ $ entity = $ entityList [0 ][' result ' ];
7880
7981 // Construct Knowledge Graph response attachment
80- $ title = $ entity [" name " ];
81- if (isset ($ entity [" description " ])) {
82- $ title = $ title . " " . $ entity [" description " ];
82+ $ title = $ entity [' name ' ];
83+ if (isset ($ entity [' description ' ])) {
84+ $ title = $ title . ' ' . $ entity [' description ' ];
8385 }
84- $ attachmentJson = [" title " => $ title ];
86+ $ attachmentJson = [' title ' => $ title ];
8587
86- if (isset ($ entity [" detailedDescription " ])) {
87- $ detailedDescJson = $ entity [" detailedDescription " ];
88+ if (isset ($ entity [' detailedDescription ' ])) {
89+ $ detailedDescJson = $ entity [' detailedDescription ' ];
8890 $ attachmentJson = array_merge ([
89- " title_link " => $ detailedDescJson [ " url " ],
90- " text " => $ detailedDescJson [" articleBody " ],
91+ ' title_link ' => $ detailedDescJson [ ' url ' ],
92+ ' text ' => $ detailedDescJson [' articleBody ' ],
9193 ], $ attachmentJson );
9294 }
9395
94- if ($ entity [" image " ]) {
95- $ imageJson = $ entity [" image " ];
96- $ attachmentJson [" image_url " ] = $ imageJson [" contentUrl " ];
96+ if ($ entity [' image ' ]) {
97+ $ imageJson = $ entity [' image ' ];
98+ $ attachmentJson [' image_url ' ] = $ imageJson [' contentUrl ' ];
9799 }
98100
99- $ responseJson [" attachments " ] = array ($ attachmentJson );
101+ $ responseJson [' attachments ' ] = array ($ attachmentJson );
100102
101103 return json_encode ($ responseJson );
102104}
103105// [END functions_slack_format]
104106
105107// [START functions_slack_request]
106108/**
107- * Send the user's search query to the Knowledge Graph API.
108- */
109+ * Send the user's search query to the Knowledge Graph API.
110+ */
109111function searchKnowledgeGraph ($ query ): Google_Service_Kgsearch_SearchResponse
110112{
111113 /**
112- * PHP Functions Framework does not support "global"/instance-scoped
113- * constants, so we fetch these values within PHP functions themselves.
114- */
114+ * PHP Functions Framework does not support "global"/instance-scoped
115+ * constants, so we fetch these values within PHP functions themselves.
116+ */
115117 $ API_KEY = getenv ("KG_API_KEY " );
116118
117119 $ apiClient = new Google \Client ();
@@ -129,31 +131,32 @@ function searchKnowledgeGraph($query): Google_Service_Kgsearch_SearchResponse
129131
130132// [START functions_slack_search]
131133/**
132- * Receive a Slash Command request from Slack.
133- */
134+ * Receive a Slash Command request from Slack.
135+ */
134136function receiveRequest (ServerRequestInterface $ request ): ResponseInterface
135137{
136138 // Validate request
137139 if ($ request ->getMethod () !== 'POST ' ) {
138140 // [] = empty headers
139- return new Response (405 , [], '' );
141+ return new Response (405 );
140142 }
141143
142- // Slack sends requests as URL-encoded strings
144+ // Parse incoming URL-encoded requests from Slack
145+ // (Slack requests use the "application/x-www-form-urlencoded" format)
143146 $ bodyStr = $ request ->getBody ();
144147 parse_str ($ bodyStr , $ bodyParams );
145148
146- if (!isset ($ bodyParams [" text " ])) {
149+ if (!isset ($ bodyParams [' text ' ])) {
147150 // [] = empty headers
148- return new Response (400 , [], '' );
151+ return new Response (400 );
149152 }
150153
151154 if (!isValidSlackWebhook ($ request , $ bodyStr )) {
152155 // [] = empty headers
153- return new Response (403 , [], '' );
156+ return new Response (403 );
154157 }
155158
156- $ query = $ bodyParams [" text " ];
159+ $ query = $ bodyParams [' text ' ];
157160
158161 // Call knowledge graph API
159162 $ kgResponse = searchKnowledgeGraph ($ query );
@@ -164,7 +167,7 @@ function receiveRequest(ServerRequestInterface $request): ResponseInterface
164167
165168 return new Response (
166169 200 ,
167- [" Content-Type " => " application/json " ],
170+ [' Content-Type ' => ' application/json ' ],
168171 $ formatted_message
169172 );
170173}
0 commit comments