|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PhpBotFramework\Core; |
|
4
|
|
|
|
|
5
|
|
|
trait Inline { |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* \addtogroup Core Core(Internal) |
|
9
|
|
|
* @{ |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
/** \brief Store id of the callback query received. */ |
|
13
|
|
|
protected $_callback_query_id; |
|
14
|
|
|
|
|
15
|
|
|
/** \brief Store id of the inline query received. */ |
|
16
|
|
|
protected $_inline_query_id; |
|
17
|
|
|
|
|
18
|
|
|
/** @} */ |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* \addtogroup Api Api Methods |
|
22
|
|
|
* @{ |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
/* \brief Answer a callback query |
|
26
|
|
|
* \details Remove the updating cirle on an inline keyboard button and showing a message/alert to the user. |
|
27
|
|
|
* It will always answer the current callback query. |
|
28
|
|
|
* @param $text <i>Optional</i>. Text of the notification. If not specified, nothing will be shown to the user, 0-200 characters. |
|
29
|
|
|
* @param $show_alert <i>Optional</i>. If true, an alert will be shown by the client instead of a notification at the top of the chat screen. |
|
30
|
|
|
* @param $url <i>Optional</i>. URL that will be opened by the user's client. If you have created a Game and accepted the conditions via @Botfather, specify the URL that opens your game – note that this will only work if the query comes from a callback_game button. |
|
31
|
|
|
* Otherwise, you may use links like telegram.me/your_bot?start=XXXX that open your bot with a parameter. |
|
32
|
|
|
* @return True on success. |
|
33
|
|
|
*/ |
|
34
|
|
|
public function answerCallbackQuery($text = '', $show_alert = false, string $url = '') : bool { |
|
35
|
|
|
|
|
36
|
|
|
if (!isset($this->_callback_query_id)) { |
|
37
|
|
|
|
|
38
|
|
|
throw new BotException("Callback query id not set, wrong update"); |
|
39
|
|
|
|
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
$parameters = [ |
|
43
|
|
|
'callback_query_id' => $this->_callback_query_id, |
|
44
|
|
|
'text' => $text, |
|
45
|
|
|
'show_alert' => $show_alert, |
|
46
|
|
|
'url' => $url |
|
47
|
|
|
]; |
|
48
|
|
|
|
|
49
|
|
|
return $this->exec_curl_request('answerCallbackQuery?' . http_build_query($parameters)); |
|
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
/* |
|
55
|
|
|
* Answer a inline query (when the user write @botusername "Query") with a button, that will make user switch to the private chat with the bot, on the top of the results (https://core.telegram.org/bots/api#answerinlinequery) |
|
56
|
|
|
* @param |
|
57
|
|
|
* $results Array on InlineQueryResult (https://core.telegram.org/bots/api#inlinequeryresult) |
|
58
|
|
|
* $switch_pm_text Text to show on the button |
|
59
|
|
|
*/ |
|
60
|
|
View Code Duplication |
public function answerInlineQuerySwitchPM($results, $switch_pm_text, $switch_pm_parameter = '', $is_personal = true, $cache_time = 300) { |
|
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
if (!isset($this->_inline_query_id)) { |
|
63
|
|
|
|
|
64
|
|
|
throw new BotException("Inline query id not set, wrong update"); |
|
65
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$parameters = [ |
|
69
|
|
|
'inline_query_id' => $this->_inline_query_id, |
|
70
|
|
|
'switch_pm_text' => $switch_pm_text, |
|
71
|
|
|
'is_personal' => $is_personal, |
|
72
|
|
|
'switch_pm_parameter' => $switch_pm_parameter, |
|
73
|
|
|
'results' => $results, |
|
74
|
|
|
'cache_time' => $cache_time |
|
75
|
|
|
]; |
|
76
|
|
|
|
|
77
|
|
|
return $this->exec_curl_request('answerInlineQuery?' . http_build_query($parameters)); |
|
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/* |
|
82
|
|
|
* Answer a inline query (when the user write @botusername "Query") with a button, that will make user switch to the private chat with the bot, on the top of the results (https://core.telegram.org/bots/api#answerinlinequery) |
|
83
|
|
|
* without showing any results to the user |
|
84
|
|
|
* @param |
|
85
|
|
|
* $switch_pm_text Text to show on the button |
|
86
|
|
|
*/ |
|
87
|
|
View Code Duplication |
public function answerEmptyInlineQuerySwitchPM($switch_pm_text, $switch_pm_parameter = '', $is_personal = true, $cache_time = 300) { |
|
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
if (!isset($this->_inline_query_id)) { |
|
90
|
|
|
|
|
91
|
|
|
throw new BotException("Inline query id not set, wrong update"); |
|
92
|
|
|
|
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$parameters = [ |
|
96
|
|
|
'inline_query_id' => $this->_inline_query_id, |
|
97
|
|
|
'switch_pm_text' => $switch_pm_text, |
|
98
|
|
|
'is_personal' => $is_personal, |
|
99
|
|
|
'switch_pm_parameter' => $switch_pm_parameter, |
|
100
|
|
|
'cache_time' => $cache_time |
|
101
|
|
|
]; |
|
102
|
|
|
|
|
103
|
|
|
return $this->exec_curl_request('answerInlineQuery?' . http_build_query($parameters)); |
|
|
|
|
|
|
104
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** @} */ |
|
108
|
|
|
|
|
109
|
|
|
} |
|
110
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.