tompedals/helpscout-dynamic-app

A library to assist the creation of Help Scout dynamic apps

0.2.1 2016-09-28 21:51 UTC

This package is auto-updated.

Last update: 2024-04-20 03:42:30 UTC


README

A simple library to verify Help Scout dynamic app requests and respond accordingly. Forked from the official library to add support for PSR-7 HTTP message interfaces.

Installation

This is installable via Composer as tompedals/helpscout-dynamic-app:

composer require tompedals/helpscout-dynamic-app

Example

Note: The following example uses zendframework/zend-diactoros as the HTTP message implementation.

Handle a request

The request will be verified using the given secret and signature from the request headers.

use TomPedals\HelpScoutApp\AppRequestFactory;
use Zend\Diactoros\ServerRequestFactory;

$factory = new AppRequestFactory('secret');
$request = $factory->create(ServerRequestFactory::fromGlobals());

/** @var TomPedals\HelpScoutApp\Model\Customer */
$customer = $request->getCustomer();

/** @var TomPedals\HelpScoutApp\Model\Mailbox */
$mailbox = $request->getMailbox();

/** @var TomPedals\HelpScoutApp\Model\Ticket */
$ticket = $request->getTicket();

/** @var TomPedals\HelpScoutApp\Model\User */
$user = $request->getUser();

Respond to a request

Set the HTML on the response and get the correct schema for the JSON response.

use TomPedals\HelpScoutApp\AppResponse;
use Zend\Diactoros\Response\JsonResponse;

$response = new AppResponse('<h4>Test</h4>');
$jsonResponse = new JsonResponse($response->getData());

Controller action

A PSR-7 compatible action is available to handle the request and respond accordingly. The action is an invokable class that can be used with Slim, Symfony, Zend Framework, etc.

Implement the AppHandlerInterface to handle the AppRequest and return the HTML to be rendered within the Help Scout sidebar.

class AppHandler implements AppHandlerInterface
{
    public function handle(AppRequest $request)
    {
        // Find customer information
        // Render the template
        // Return the HTML response

        return '<h4>This customer is awesome</h4>';
    }
}

Pass the AppHandler implementation when constructing the action.

use TomPedals\HelpScoutApp\AppAction;
use TomPedals\HelpScoutApp\AppHandlerInterface;
use TomPedals\HelpScoutApp\AppRequestFactory;

// implements AppHandlerInterface
$handler = new AppHandler();

$action = new AppAction(new AppRequestFactory('secret'), $handler);
$response = $action($request, $response);