@@ -139,16 +139,195 @@ EOF
139
139
, //import your private_key
140
140
];
141
141
```
142
- Replace the value with **YOUR APP** mixin_id, client_id, client_secret, sessionId, and the pin, pin token, session_id, private key you already generated them in dashboard.
142
+ Replace the value with **YOUR APP** mixin_id, client_id, client_secret, and the pin, pin token, session_id, private key you have already generated them in dashboard.
143
143
144
- ### 测试
144
+ ### Hello world
145
+ Fill the following content in app.php, create it if it is missing in your folder
146
+ ```php
147
+ require __DIR__ . ' /vendor/autoload.php' ;
148
+ use ExinOne\MixinSDK\Traits\MixinSDKTrait;
149
+ use ExinOne\MixinSDK\MixinSDK;
150
+ use Ramsey\Uuid\Uuid;
151
+ use Ratchet\RFC6455\Messaging\Frame;
152
+
153
+ $loop = \React\EventLoop\Factory::create();
154
+ $reactConnector = new \React\Socket\Connector($loop, [
155
+ ' timeout' => 15
156
+ ]);
157
+ class callTraitClass {
158
+ use MixinSDKTrait;
159
+ public $config;
160
+ public function __construct()
161
+ {
162
+ $config = require(__DIR__.'/config.php');
163
+ $this->config = $config;
164
+ }
165
+ }
166
+ $callTrait = new callTraitClass();
167
+ $Token = $callTrait->getToken('GET', '/', '');
168
+ print_r($callTrait->config['client_id']);
169
+ // $Header = 'Authorization'.'Bearer '.$Token;
170
+ // print($Header);
171
+ $connector = new \Ratchet\Client\Connector($loop,$reactConnector);
172
+ // $connector('ws://127.0.0.1:9000', ['protocol' => 'Mixin-Blaze-1'], ['Origin' => 'http://localhost',
173
+ $connector('wss://blaze.mixin.one', ['protocol' => 'Mixin-Blaze-1'],[
174
+ 'Authorization' => 'Bearer '.$Token
175
+ ])
176
+ ->then(function(Ratchet\Client\WebSocket $conn) {
177
+ $conn->on('message', function(\Ratchet\RFC6455\Messaging\MessageInterface $msg) use ($conn) {
178
+ $jsMsg = json_decode(gzdecode($msg));
179
+ print_r($jsMsg);
180
+ if ($jsMsg->action === 'CREATE_MESSAGE' and property_exists($jsMsg,'data')) {
181
+ echo "\nNeed reply server a receipt!\n";
182
+ $RspMsg = generateReceipt($jsMsg->data->message_id);
183
+ $msg = new Frame(gzencode(json_encode($RspMsg)),true,Frame::OP_BINARY);
184
+ $conn->send($msg);
185
+
186
+ if ($jsMsg->data->category === 'PLAIN_TEXT') {
187
+ $msgData = sendPlainText($jsMsg->data->conversation_id,
188
+ base64_decode($jsMsg->data->data));
189
+ $msg = new Frame(gzencode(json_encode($msgData)),true,Frame::OP_BINARY);
190
+ $conn->send($msg);
191
+ } //end of PLAIN_TEXT
192
+ } //end of CREATE_MESSAGE
193
+
194
+ });
195
+ $conn->on('close', function($code = null, $reason = null) {
196
+ echo "Connection closed ({$code} - {$reason})\n";
197
+ });
198
+ /* start listen for the incoming message */
199
+ $message = [
200
+ 'id' => Uuid::uuid4()->toString(),
201
+ 'action' => 'LIST_PENDING_MESSAGES',
202
+ ];
203
+ print_r(json_encode($message));
204
+ $msg = new Frame(gzencode(json_encode($message)),true,Frame::OP_BINARY);
205
+ $conn->send($msg);
206
+ // $conn->send(gzencode($msg,1,FORCE_DEFLATE));
207
+ }, function(\Exception $e) use ($loop) {
208
+ echo "Could not connect: {$e->getMessage()}\n";
209
+ $loop->stop();
210
+ });
211
+
212
+ $loop->run();
213
+
214
+
215
+ function sendPlainText($conversation_id,$msgContent):Array {
216
+
217
+ $msgParams = [
218
+ 'conversation_id' => $conversation_id,
219
+ 'category' => 'PLAIN_TEXT',
220
+ 'status' => 'SENT',
221
+ 'message_id' => Uuid::uuid4()->toString(),
222
+ 'data' => base64_encode($msgContent),//base64_encode("hello!"),
223
+ ];
224
+ $msgPayButton = [
225
+ 'id' => Uuid::uuid4()->toString(),
226
+ 'action' => 'CREATE_MESSAGE',
227
+ 'params' => $msgParams,
228
+ ];
229
+ return $msgPayButton;
230
+ }
231
+
232
+ function generateReceipt($msgID):Array {
233
+ $IncomingMsg = ["message_id" => $msgID, "status" => "READ"];
234
+ $RspMsg = ["id" => Uuid::uuid4()->toString(), "action" => "ACKNOWLEDGE_MESSAGE_RECEIPT",
235
+ "params" => $IncomingMsg];
236
+ return $RspMsg;
237
+ }
238
+ ```
239
+ Run the app.php
240
+ ``` bash
241
+ php app.php
242
+ ```
243
+ If everything is ok, the following content will be display
145
244
``` bash
146
- php chat-client.php
245
+ wenewzha:mixin_labs-php-bot wenewzhang$ php helloworld.php
246
+ a1ce2967-a534-417d-bf12-c86571e4eefa{" id" :" 4454b6c5-4a89-440c-bd22-7a79cf4954ca" ," action" :" LIST_PENDING_MESSAGES" }stdClass Object
247
+ (
248
+ [id] => 4454b6c5-4a89-440c-bd22-7a79cf4954ca
249
+ [action] => LIST_PENDING_MESSAGES
250
+ )
147
251
```
252
+ In [ Mixin Messenger] ( https://mixin.one/ ) ,add the bot as your friend,(for example, this bot id is 7000101639) and then send any text!
148
253
149
- ## wrenchClient连接chat服务器的方法
150
- composer install
151
- php chat.php
152
- php wrenchClient.php
153
- test:
154
- .. /.. /vendor /bin /phpunit MessageApiTest.php
254
+ ![ mixin_messenger] ( https://github.com/wenewzhang/mixin_labs-php-bot/blob/master/helloworld.jpeg )
255
+
256
+
257
+ ### Source code explanation
258
+ The WebSocket providing full-duplex communication channels over a single TCP connection, It is a persistence connection, so create loop for the connection.
259
+ ``` php
260
+ $loop = \React\EventLoop\Factory::create();
261
+ $reactConnector = new \React\Socket\Connector($loop, [
262
+ 'timeout' => 15
263
+ ]);
264
+ $connector = new \Ratchet\Client\Connector($loop,$reactConnector);
265
+ ```
266
+
267
+ To receive message from Mixin messenger user, the application need to create a connection to Mixin Messenger server. The application also need to create a token which is used in later communication.
268
+
269
+ [ API of the operation] ( https://developers.mixin.one/api/beta-mixin-message/authentication/ ) , [ Guide of the operation] ( https://mixin-network.gitbook.io/mixin-network/mixin-messenger-app/receive-asset-change-notification )
270
+
271
+ The mixin-sdk-php implements the getToken function, call it and generate token here.
272
+ ``` php
273
+ class callTraitClass {
274
+ use MixinSDKTrait;
275
+ public $config;
276
+ public function __construct()
277
+ {
278
+ $config = require(__DIR__.'/config.php');
279
+ $this->config = $config;
280
+ }
281
+ }
282
+ $callTrait = new callTraitClass();
283
+ $Token = $callTrait->getToken('GET', '/', '');
284
+ ```
285
+ Connect to the mixin.one server.
286
+ ``` php
287
+ $connector('wss://blaze.mixin.one', ['protocol' => 'Mixin-Blaze-1'],[
288
+ 'Authorization' => 'Bearer '.$Token
289
+ ])
290
+ ```
291
+ Then add ** onMessage** to receive and analyze the incoming messages
292
+ ``` php
293
+ ->then(function(Ratchet\Client\WebSocket $conn) {
294
+ $conn->on('message', function(\Ratchet\RFC6455\Messaging\MessageInterface $msg) use ($conn) {
295
+ $jsMsg = json_decode(gzdecode($msg));
296
+ print_r($jsMsg);
297
+ if ($jsMsg->action === 'CREATE_MESSAGE' and property_exists($jsMsg,'data')) {
298
+ echo "\nNeed reply server a receipt!\n";
299
+ $RspMsg = generateReceipt($jsMsg->data->message_id);
300
+ $msg = new Frame(gzencode(json_encode($RspMsg)),true,Frame::OP_BINARY);
301
+ $conn->send($msg);
302
+
303
+ if ($jsMsg->data->category === 'PLAIN_TEXT') {
304
+ $msgData = sendPlainText($jsMsg->data->conversation_id,
305
+ base64_decode($jsMsg->data->data));
306
+ $msg = new Frame(gzencode(json_encode($msgData)),true,Frame::OP_BINARY);
307
+ $conn->send($msg);
308
+ } //end of PLAIN_TEXT
309
+ } //end of CREATE_MESSAGE
310
+
311
+ });
312
+ $conn->on('close', function($code = null, $reason = null) {
313
+ echo "Connection closed ({$code} - {$reason})\n";
314
+ });
315
+ ```
316
+ Not only text messages, images and other type message can be received. You can find message details in [ Here] ( https://developers.mixin.one/api/beta-mixin-message/websocket-messages/ ) .
317
+
318
+ Send the READ message to the server let it knows this message has already been read. If you don't send it, the bot will receive the duplicated message again after the bot connect to server again!
319
+ ``` php
320
+ echo "\nNeed reply server a receipt!\n";
321
+ $RspMsg = generateReceipt($jsMsg->data->message_id);
322
+ $msg = new Frame(gzencode(json_encode($RspMsg)),true,Frame::OP_BINARY);
323
+ $conn->send($msg);
324
+
325
+ function generateReceipt($msgID):Array {
326
+ $IncomingMsg = ["message_id" => $msgID, "status" => "READ"];
327
+ $RspMsg = ["id" => Uuid::uuid4()->toString(), "action" => "ACKNOWLEDGE_MESSAGE_RECEIPT",
328
+ "params" => $IncomingMsg];
329
+ return $RspMsg;
330
+ }
331
+ ```
332
+ ### End
333
+ Now your bot is running. You can try your idea now,enjoy!
0 commit comments