TextPlugin::request()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 11
nc 1
nop 2
1
<?php
2
3
namespace Nopolabs\Yabot\Plugins\Sms;
4
5
use React\Http\Request as HttpRequest;
6
use React\Http\Response as HttpResponse;
7
use Nopolabs\Yabot\Message\Message;
8
use Nopolabs\Yabot\Plugin\PluginInterface;
9
use Nopolabs\Yabot\Plugin\PluginTrait;
10
use Nopolabs\Yabot\Slack\Client;
11
use Nopolabs\Yabot\Helpers\SlackTrait;
12
use Nopolabs\Yabot\Http\HttpServer;
13
use Psr\Log\LoggerInterface;
14
use React\Stream\BufferedSink;
15
16
class TextPlugin implements PluginInterface
17
{
18
    use PluginTrait;
19
    use SlackTrait;
20
21
    /** @var TextClient */
22
    private $textClient;
23
    private $fromPhone;
24
25
    public function __construct(
26
        LoggerInterface $logger,
27
        Client $slack,
28
        HttpServer $http,
29
        TextClient $textClient,
30
        array $config)
31
    {
32
        $this->setLog($logger);
33
        $this->setSlack($slack);
34
        $http->addHandler([$this, 'request']);
35
        $this->textClient = $textClient;
36
        $this->fromPhone = $config['twilio']['phone'];
37
38
        $this->setConfig(array_merge(
39
            [
40
                'help' => '<prefix> text [number] [message]',
41
                'matchers' => [
42
                    'text' => [
43
                        'pattern' => "/^text (?'number'\\d{3}-?\\d{3}-?\\d{4})\\b(?'message'.*)$/",
44
                        'method' => 'text',
45
                    ],
46
                ],
47
            ],
48
            $config
49
        ));
50
    }
51
52
    public function text(Message $msg, array $matches)
0 ignored issues
show
Unused Code introduced by
The parameter $msg is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
53
    {
54
        $to = '+1'.str_replace('-', '', $matches['number']);
55
        $message = $matches['message'];
56
57
        $this->textClient->send($to, $this->fromPhone, $message);
58
    }
59
60
    /**
61
     * Need to configure twilio phone number messaging URL: https://www.twilio.com/console/phone-numbers/incoming
62
     * For testing I use ngrok to tunnel local ports to public URLs and inspect traffic https://ngrok.com/
63
     *
64
     * @param HttpRequest $request
65
     * @param HttpResponse $response
66
     */
67
    public function request(HttpRequest $request, HttpResponse $response)
68
    {
69
        $headers = array('Content-Type' => 'text/plain');
70
        $response->writeHead(200, $headers);
71
72
        $sink = new BufferedSink();
73
        $request->pipe($sink);
74
        $sink->promise()->then(function ($data) use ($response) {
75
            if (mb_parse_str($data, $result)) {
76
                $from = $result['From'];
77
                $body = $result['Body'];
78
                $this->say("$from: $body", 'general');
79
            }
80
            $response->end();
81
        });
82
    }
83
}