Updates   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 12.5%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 0
dl 0
loc 86
rs 10
c 0
b 0
f 0
ccs 2
cts 16
cp 0.125

6 Methods

Rating   Name   Duplication   Size   Complexity  
execRequest() 0 1 ?
A setWebhook() 0 5 1
A getWebhookInfo() 0 5 1
A deleteWebhook() 0 5 1
A getUpdates() 0 11 1
A setUpdateReturned() 0 14 1
1
<?php
2
3
/*
4
 * This file is part of the PhpBotFramework.
5
 *
6
 * PhpBotFramework is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Lesser General Public License as
8
 * published by the Free Software Foundation, version 3.
9
 *
10
 * PhpBotFramework is distributed in the hope that it will be useful, but
11
 * WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
 * Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public License
16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
namespace PhpBotFramework\Core;
20
21
/**
22
 * \class Updates
23
 * \brief All API Methods that get updates from telegram or handle webhooks.
24
 */
25
trait Updates
26
{
27
    abstract protected function execRequest(string $url);
28
29
    /**
30
     * \addtogroup Api Api Methods
31
     * @{
32
     */
33
34
    /**
35
     * \brief Set bot's webhook.
36
     * \details Set a webhook for the current bot in order to receive incoming
37
     * updates via an outgoing webhook.
38
     * @param $params See [Telegram API](https://core.telegram.org/bots/api#setwebhook)
39
     * for more information about the available parameters.
40
     */
41
    public function setWebhook(array $params)
42
    {
43
44
        return $this->execRequest('setWebhook?' . http_build_query($params));
45
    }
46
47
    /**
48
     * \brief Get information about bot's webhook.
49
     * \details Returns an hash which contains information about bot's webhook.
50
     * @return Array|false Webhook info.
51
     */
52 1
    public function getWebhookInfo()
53
    {
54
55 1
        return $this->execRequest('getWebhookInfo');
56
    }
57
58
    /**
59
     * \brief Delete bot's webhook.
60
     * \details Delete bot's webhook if it exists.
61
     */
62
    public function deleteWebhook()
63
    {
64
65
        return $this->execRequest('deleteWebhook');
66
    }
67
68
    /**
69
     * \brief Request bot updates.
70
     * \details Request updates received by the bot using method getUpdates of Telegram API. [API reference](https://core.telegram.org/bots/api#getupdates)
71
     * @param int $offset <i>Optional</i>. Identifier of the first update to be returned. Must be greater by one than the highest among the identifiers of previously received updates. By default, updates starting with the earliest unconfirmed update are returned. An update is considered confirmed as soon as getUpdates is called with an offset higher than its update_id. The negative offset can be specified to retrieve updates starting from -offset update from the end of the updates queue. All previous updates will forgotten.
72
     * @param int $limit <i>Optional</i>. Limits the number of updates to be retrieved. Values between 1—100 are accepted.
73
     * @param int $timeout <i>Optional</i>. Timeout in seconds for long polling.
74
     * @return Array|false Array of updates (can be empty).
75
     */
76
    public function getUpdates(int $offset = 0, int $limit = 100, int $timeout = 60)
77
    {
78
79
        $parameters = [
80
            'offset' => $offset,
81
            'limit' => $limit,
82
            'timeout' => $timeout,
83
        ];
84
85
        return $this->execRequest('getUpdates?' . http_build_query($parameters));
86
    }
87
88
    /**
89
     * \brief Set updates received by the bot for getUpdates handling.
90
     * \details List the types of updates you want your bot to receive. For example, specify [“message”, “edited_channel_post”, “callback_query”] to only receive updates of these types. Specify an empty list to receive all updates regardless of type.
91
     * Set it one time and it won't change until next setUpdateReturned call.
92
     * @param Array $allowed_updates <i>Optional</i>. List of updates allowed.
93
     */
94
    public function setUpdateReturned(array $allowed_updates = [])
95
    {
96
97
        // Parameter for getUpdates
98
        $parameters = [
99
            'offset' => 0,
100
            'limit' => 1,
101
            'timeout' => 0,
102
        ];
103
104
        // Exec getUpdates
105
        $this->execRequest('getUpdates?' . http_build_query($parameters)
106
                                               . '&allowed_updates=' . json_encode($allowed_updates));
107
    }
108
109
    /** @} */
110
}
111