Skip to content

Instantly share code, notes, and snippets.

@boulama
Created April 26, 2022 08:40
Show Gist options
  • Save boulama/a1962eb0124bf2d6b23b40befd63e1b9 to your computer and use it in GitHub Desktop.
Save boulama/a1962eb0124bf2d6b23b40befd63e1b9 to your computer and use it in GitHub Desktop.
feedbucket webhook
<?php
use GuzzleHttp\Client;
$client = new Client();
$data = json_decode($request->getContent(), true);
$action = $data['action'];
$feedback = $data['feedback'];
$project = $feedback['project'];
$reporter = $feedback['reporter'];
if($action == 'feedback_created') {
$reporter_data = "{$reporter['name']} ({$reporter['email']})";
$project_data = "{$project['name']} ({$project['url']})";
$feedback_text = $feedback['text'];
$feedback_id = intval($feedback['id']);
/*
* use Tinq to get labels using our custom classifier
* classifier tutorial: https://www.youtube.com/watch?v=lbsysOlrxQc
* classifier dashboard: https://tinq.ai/tools/classifiers/
* API key: https://tinq.ai/dashboard/profile
*/
$tinqClassifierID = '<tinq-classifier-id>';
$tinqAPIKey = '<tinq-api-key>';
$classifierResponse = $client->request('POST', 'https://tinq.ai/api/v1/classifier', [
'form_params' => [
'text' => $feedback_text,
'classifier' => $tinqClassifierID
],
'headers' => [
'Accept' => 'application/json',
'Authorization' => "Bearer $tinqAPIKey",
'Content-Type' => 'application/x-www-form-urlencoded',
],
]);
// get the json object
$classifierBody = $classifierResponse->getBody();
// convert JSON object to php array
$classifierBodyArray = json_decode($classifierBody, true);
$dominantLabel = $classifierBodyArray['dominant_label'];
$classifierLabel = $dominantLabel['label'];
// Airtable API:
$airtableAPIKey = '<airtable-api-key>';
$airtableTableURL = "<airtable-base-url>";
$airtableRequest = new \GuzzleHttp\Psr7\Request(
"POST",
$airtableTableURL,
[
"Authorization" => "Bearer $airtableAPIKey",
"Content-Type" => "application/json",
],
"
{
\"fields\":
{
\"feedback_id\":$feedback_id,
\"project\":\"$project_data\",
\"reporter\":\"$reporter_data\",
\"feedback\":\"$feedback_text\",
\"attachment\":\"{$feedback['resource']}\",
\"label\":\"$classifierLabel\"
}
}");
\Illuminate\Support\Facades\Log::alert("OK - WEBHOOK FIRED");
$airtableResponse = $client->send($airtableRequest);
return response()->json($airtableResponse);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment