-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.php
155 lines (130 loc) · 4.62 KB
/
example.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
require_once './vendor/autoload.php';
// Just in case you have not installed the awesome 'symfony/var-dumper' package for beautiful dump outputs:
if (!function_exists('dump')) {
function dump(mixed ...$vars)
{
var_dump('dump:', ...$vars);
}
}
// * IMPORTANT: Look for 'test_data.sample.php' in the same directory and fill it with required information, then rename to 'test_data.php'
require_once dirname(__FILE__) . '/test_data.php'; // Includes $token, $some_chat_id and $options
// --- --- --- --- --- --- ---
use Litegram\Update;
use Litegram\CopyMessageParams;
use Litegram\InlineKeyboardButton;
use Litegram\InlineKeyboardMarkup;
use Litegram\InputFile;
use Litegram\ReplyParameters;
use Litegram\TelegramMethods;
use Litegram\SendMessageParams;
use Litegram\SendPhotoParams;
try {
// TODO: Add example for receiving updates manually (via getUpdates method)
// runExampleForReceivingUpdatesFromWebhook();
runExampleForGetMe();
$sentMessage1Id = runExampleForSendMessage();
$sentMessage2Id = runExampleForSendPhoto();
runExampleFor_bulkCopyMessage($sentMessage1Id, $sentMessage2Id);
} catch (\Throwable $th) {
dump('Exception:', $th);
}
function runExampleForReceivingUpdatesFromWebhook()
{
// * IMPORTANT: Make sure you have set up webhook
$update_str = file_get_contents('php://input');
if (!$update_str) {
error_log('No update was received from webhook!');
exit(0);
}
try {
$update = new Update(init_data: json_decode($update_str));
dump($update);
} catch (\Throwable $th) {
dump('Exception:', $th);
}
}
function runExampleForGetMe()
{
global $token, $guzzle_options;
// If the request doesn't fail, an object of type Litegram\User will be returned
$res = TelegramMethods::getMe(
token: $token,
guzzle_options: $guzzle_options,
);
dump($res);
}
function runExampleForSendMessage()
{
global $token, $some_chat_id, $guzzle_options;
// If the request doesn't fail, an object of type Litegram\Message will be returned
$res = TelegramMethods::sendMessage(
token: $token,
params: new SendMessageParams(
chat_id: $some_chat_id,
text: 'Test',
reply_markup: new InlineKeyboardMarkup([
[
new InlineKeyboardButton('Hi', callback_data: 'say_hi'),
new InlineKeyboardButton('Bye', callback_data: 'say_bye'),
],
[new InlineKeyboardButton('Close', callback_data: 'close')],
]),
),
guzzle_options: $guzzle_options,
);
dump($res);
return $res->message_id;
}
function runExampleForSendPhoto()
{
global $token, $some_chat_id, $guzzle_options;
// If the request doesn't fail, an object of type Litegram\Message will be returned
$res = TelegramMethods::sendPhoto(
token: $token,
params: new SendPhotoParams(
chat_id: $some_chat_id,
photo: new InputFile('/home/amir/test.jpg'),
caption: 'Look at this beautiful landscape!',
show_caption_above_media: true,
),
guzzle_options: $guzzle_options,
);
dump($res);
return $res->message_id;
}
function runExampleFor_bulkCopyMessage(int $message1Id, int $message2Id)
{
global $token, $some_chat_id, $guzzle_options;
// Returns a promise of type GuzzleHttp\Promise\PromiseInterface.
// Make sure to use ->wait method to pause execution until the every promise is either resolved or rejected
$promise = TelegramMethods::_bulkCopyMessage(
token: $token,
array_of_params: [
new CopyMessageParams(
chat_id: $some_chat_id,
from_chat_id: $some_chat_id,
message_id: $message1Id,
caption: 'Copied the message and changed the caption!',
reply_parameters: new ReplyParameters(
message_id: $message1Id,
chat_id: $some_chat_id,
allow_sending_without_reply: true,
),
),
new CopyMessageParams(
chat_id: $some_chat_id,
from_chat_id: $some_chat_id,
message_id: $message2Id,
caption: 'Copied the message and changed the caption!',
reply_parameters: new ReplyParameters(
message_id: $message2Id,
chat_id: $some_chat_id,
allow_sending_without_reply: true,
),
),
],
guzzle_options: $guzzle_options,
);
dump($promise->wait());
}