Completed
Push — master ( 615879...2eb041 )
by Vladimir
02:48
created

TelegramDriver::sendMessage()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 16
ccs 8
cts 8
cp 1
rs 9.4285
cc 2
eloc 9
nc 3
nop 3
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FondBot\Channels\Telegram;
6
7
use GuzzleHttp\Client;
8
use FondBot\Contracts\Channels\User;
9
use FondBot\Contracts\Channels\Driver;
10
use GuzzleHttp\Exception\RequestException;
11
use FondBot\Contracts\Conversation\Keyboard;
12
use FondBot\Contracts\Channels\OutgoingMessage;
13
use FondBot\Contracts\Channels\ReceivedMessage;
14
use FondBot\Channels\Exceptions\InvalidChannelRequest;
15
use FondBot\Contracts\Channels\Extensions\WebhookInstallation;
16
17
class TelegramDriver extends Driver implements WebhookInstallation
18
{
19
    private $guzzle;
20
21 22
    public function __construct(Client $guzzle)
22
    {
23 22
        $this->guzzle = $guzzle;
24 22
    }
25
26
    /**
27
     * Configuration parameters.
28
     *
29
     * @return array
30
     */
31 1
    public function getConfig(): array
32
    {
33
        return [
34 1
            'token',
35
        ];
36
    }
37
38
    /**
39
     * Verify incoming request data.
40
     *
41
     * @throws InvalidChannelRequest
42
     */
43 3
    public function verifyRequest(): void
44
    {
45 3
        if ($this->hasRequest('callback_query')) {
46
            return;
47
        }
48
49
        if (
50 3
            !$this->hasRequest('message') ||
51 3
            !$this->hasRequest('message.from')
52
        ) {
53 2
            throw new InvalidChannelRequest('Invalid payload');
54
        }
55 1
    }
56
57
    /**
58
     * Initialize webhook in the external service.
59
     *
60
     * @param string $url
61
     */
62 1
    public function installWebhook(string $url): void
63
    {
64 1
        $this->guzzle->post($this->getBaseUrl().'/setWebhook', [
65
            'form_params' => [
66 1
                'url' => $url,
67
            ],
68
        ]);
69 1
    }
70
71
    /**
72
     * Get message sender.
73
     *
74
     * @return User
75
     */
76 1
    public function getUser(): User
77
    {
78 1
        if ($this->hasRequest('callback_query')) {
79
            $from = $this->getRequest('callback_query.from');
80
        } else {
81 1
            $from = $this->getRequest('message.from');
82
        }
83
84 1
        return new TelegramUser($from);
85
    }
86
87
    /**
88
     * Get message received from sender.
89
     *
90
     * @return ReceivedMessage
91
     */
92 12
    public function getMessage(): ReceivedMessage
93
    {
94 12
        return new TelegramReceivedMessage(
95 12
            $this->guzzle,
96 12
            $this->getParameter('token'),
97 12
            $this->getRequest()
98
        );
99
    }
100
101
    /**
102
     * Send reply to participant.
103
     *
104
     * @param User          $sender
105
     * @param string        $text
106
     * @param Keyboard|null $keyboard
107
     *
108
     * @return OutgoingMessage
109
     */
110 3
    public function sendMessage(User $sender, string $text, Keyboard $keyboard = null): OutgoingMessage
111
    {
112 3
        $message = new TelegramOutgoingMessage($sender, $text, $keyboard);
113
114
        try {
115 3
            $this->debug('sendMessage', $message->toArray());
116
117 3
            $this->guzzle->post($this->getBaseUrl().'/sendMessage', [
118 3
                'form_params' => $message->toArray(),
119
            ]);
120 1
        } catch (RequestException $exception) {
121 1
            $this->error(get_class($exception), [$exception->getMessage()]);
122
        }
123
124 3
        return $message;
125
    }
126
127 4
    private function getBaseUrl(): string
128
    {
129 4
        return 'https://api.telegram.org/bot'.$this->getParameter('token');
130
    }
131
}
132