Skip to content

Commit 710b062

Browse files
committed
echo message,first article
1 parent d965b95 commit 710b062

File tree

3 files changed

+146
-21
lines changed

3 files changed

+146
-21
lines changed

README.md

Lines changed: 122 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
1-
# mixin_labs-php-bot
2-
Php bot for Mixin Network
1+
# Mixin Messenger application development tutorial in PHP
2+
This tutorial will let you know how to write a Mixin Messenger bot in PHP. The bot can receive and response to user's message. User can pay token to bot and bot can transfer token to user.
3+
4+
## Index
5+
1. [Create bot and receive message from user](https://github.com/wenewzhang/mixin_labs-php-bot#create-bot-and-receive-message-from-user)
6+
2. [Receive token and pay token](https://github.com/wenewzhang/mixin_labs-php-bot/blob/master/README2.md)
7+
8+
## Create bot and receive message from user
9+
You will create a bot in Mixin Messenger to receive user message after read the chapter.
10+
11+
12+
### PHP enviroment setup:
13+
This tutorial is written in PHP 7. So you need to install PHP, composer before writing code.
314

415
on macOS
516
```bash
617
brew update
7-
brew install php
18+
brew install php@7.2
819
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
920
php -r "if (hash_file('sha384', 'composer-setup.php') === '93b54496392c062774670ac18b134c3b3a95e5a5e5c8f1a9f115f203b75bf9a129d5daa8ba6a13e2cc8a1da0806388a8') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
1021
//install composer to /usr/local/opt/[email protected]/bin and give a brief name 'composer'
@@ -15,18 +26,123 @@ on Ubuntu
1526
```bash
1627
apt update
1728
apt upgrade
18-
apt install php
29+
30+
//install php 7.2
31+
apt-get install software-properties-common python-software-properties
32+
add-apt-repository -y ppa:ondrej/php
33+
apt-get update
34+
apt-get install php7.2 php7.2-cli php7.2-common
35+
//install composer
1936
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
2037
php -r "if (hash_file('sha384', 'composer-setup.php') === '93b54496392c062774670ac18b134c3b3a95e5a5e5c8f1a9f115f203b75bf9a129d5daa8ba6a13e2cc8a1da0806388a8') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
2138
//install composer to /usr/local/bin and give a brief name 'composer'
2239
php composer-setup.php --install-dir=/usr/local/bin --filename=composer
2340
php -r "unlink('composer-setup.php');"
2441
```
25-
26-
# 安装依赖包
42+
Make sure the install directory is include in the $PATH variable, run **php -v** and **composer -V** can check the installation, if console output like below, that's mean it's works!
43+
```bash
44+
wenewzha:minecraft wenewzhang$ php -v
45+
PHP 7.2.13 (cli) (built: Dec 7 2018 10:41:23) ( NTS )
46+
Copyright (c) 1997-2018 The PHP Group
47+
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
48+
with Zend OPcache v7.2.13, Copyright (c) 1999-2018, by Zend Technologies
49+
wenewzha:minecraft wenewzhang$ composer -V
50+
Composer version 1.8.0 2018-12-03 10:31:16
51+
```
52+
### Create the project
53+
Go to your documents then make a directory and name it, for example: **mixin_labs-php-bot**
54+
```bash
55+
mkdir mixin_labs-php-bot
56+
mixin_labs-php-bot
57+
```
58+
Execute **composer init** in your project directory, according the prompt to create the composer.json,
59+
```bash
60+
root@iZj6cbmqen2lqp7l48nfgkZ:~/mixin_labs-php-bot# composer init
61+
Welcome to the Composer config generator
62+
This command will guide you through creating your composer.json config.
63+
Package name (<vendor>/<name>) [user/mixin_labs-php-bot]:
64+
Description []: PHP 7 bot for Mixin Messenger
65+
Author [, n to skip]: JimmyZhang <[email protected]>
66+
Minimum Stability []:
67+
Package Type (e.g. library, project, metapackage, composer-plugin) []:
68+
License []:
69+
Define your dependencies.
70+
Would you like to define your dependencies (require) interactively [yes]? no
71+
Would you like to define your dev dependencies (require-dev) interactively [yes]? no
72+
{
73+
"name": "user/mixin_labs-php-bot",
74+
"description": "PHP 7 bot for Mixin Messenger",
75+
"authors": [
76+
{
77+
"name": "JimmyZhang",
78+
"email": "[email protected]"
79+
}
80+
],
81+
"require": {}
82+
}
83+
Do you confirm generation [yes]? yes
84+
```
85+
This tutorial need a library [mixin-sdk-php](https://github.com/ExinOne/mixin-sdk-php) and [Ratchet pawl](https://github.com/ratchetphp/Pawl), **mixin-sdk-php** is a PHP SDK for Mixin Network, the **Ratchet pawl** is a asynchronous websocket client.
86+
So, add them in the "require" block.
87+
```bash
88+
"require": {
89+
"exinone/mixin-sdk-php": "^1.1",
90+
"ratchet/pawl": "^0.3.3",
91+
},
92+
```
93+
Save the composer.json, then execute **composer install** to download the packages.
2794
```bash
2895
composer install
2996
```
97+
After the downloading finished, you can find a vendor under the project directory.
98+
```bash
99+
root@iZj6cbmqen2lqp7l48nfgkZ:~/mixin_labs-php-bot# ls
100+
composer.json composer.lock vendor
101+
```
102+
If you clone this repository, just execute **composer install** to download all dependency packages.
103+
104+
### Create you first app in developer dashboard
105+
Create an app by following [tutorial](https://mixin-network.gitbook.io/mixin-network/mixin-messenger-app/create-bot-account).
106+
107+
### Generate parameter for your app
108+
Remember to [generate parameter](https://mixin-network.gitbook.io/mixin-network/mixin-messenger-app/create-bot-account#generate-secure-parameter-for-your-app)
109+
and write down required information, they are required in config.php file soon.
110+
111+
112+
113+
![mixin_network-keys](https://github.com/wenewzhang/mixin_network-nodejs-bot2/blob/master/mixin_network-keys.jpg)
114+
In the folder, create a file: config.php. Copy the following content into it.
115+
> config.php
116+
```php
117+
return [
118+
'mixin_id' => '7000101716',
119+
'client_id' => 'a1ce2967-a534-417d-bf12-c86571e4eefa',
120+
'client_secret' => '7339866727d24eeec1c4ebb6c634fd25a7b9057ee6d5939cca9b6b9fc15f4d1f',
121+
'pin' => '512772',
122+
'pin_token' => 'abRdNq6soRALRG434IgR7WS/qP7LOcpfviqSfWfABdIKyZGLnWXFMrVCHpChIkBRGRAcsUguni0OoNsShddPVL3qoD5fxbF5dRUiRv14urH1Pmdl6zIZdCH159QMr5wLmmSHSGu2AihNkUHUo3bAJsrvOW0nke5y6R5YE/pNNfo=',
123+
'session_id' => '51faabbf-48ff-4df2-898d-e9b318afae35',
124+
'private_key' => <<<EOF
125+
-----BEGIN RSA PRIVATE KEY-----
126+
MIICXQIBAAKBgQCuKI65sJR9lQ1+kyKouWu3CpmkPdJKaFqKVMEWk9RRH1Wgju9n
127+
z/y5MiBVZKUeeIYtwrCNKbbdkSPqMoj1kLh5XUk4HaV9DUt+s9USBHOgU8m5Pxov
128+
Km+HQ+Pam62lHWn6ClYaNrDihpcdDg9i7Y8hY1cgKiUcdkFQmDQ9lz2VHwIDAQAB
129+
AoGANHJSSOk8TnVMkwmMLnNoVL8EdcmIQpAac/4CB+KM1cEx8CAbSJAB82N9CTo9
130+
32c8QRuYP2qIf0DuJ+EADbN/Wc3o9zRY3dkbnLo144g3YaKwDccSgUMux03ANHlP
131+
MEPDxOUbxJTRPXmKgUZmGJrkAClGbr3pPyQDDHDWRQc9JUECQQDT7pUYcXtu+hSc
132+
nAlZllzqkBG2gZrDYpPJ0JirpfNhaApBo+CGZYKQ1961o6+HcI9gZmZA8hPEhT6p
133+
PlubjqxbAkEA0l89du8TIUGrY9/sxyfZif6aeEztXPwBHZ9r8dm0L8Mlu5zTrOX2
134+
SUgu3znM6djmuRMS45iPHJbPkvw9ilaljQJBAJRN323Ec/D79ZKGKpDThN/rw0lo
135+
tolFoU/Xtg5fycl/CbZXXFYQEOcU+Nc43Ss1HFAEOEf4Xtbluyyp42ce1wMCQElv
136+
P4htyhK41rglaYTXr0NRYeCOkej8evM5PDgPU6u8hkZoZyeamo9YKCx6A8K5mUiP
137+
lO9nyMUlC852SJEqz90CQQDBguGg5GGcfehpIZwERlMJgKGg1+13/9GfnEPdAW2v
138+
px7DZoMG/pQ/SEa53tJHmGGD9+qyp93z/fEPXsD5RSwx
139+
-----END RSA PRIVATE KEY-----
140+
EOF
141+
, //import your private_key
142+
];
143+
```
144+
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.
145+
30146
### 测试
31147
```bash
32148
php chat-client.php

app.php

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,25 +44,29 @@ public function __construct()
4444
echo "PLAIN_TEXT:".base64_decode($jsMsg->data->data);
4545
$isCmd = strtolower(base64_decode($jsMsg->data->data));
4646
if ($isCmd ==='?' or $isCmd ==='help') {
47-
$msgData = sendPlainText($jsMsg);
47+
$msgData = sendUsage($jsMsg->data->conversation_id);
4848
$msg = new Frame(gzencode(json_encode($msgData)),true,Frame::OP_BINARY);
4949
$conn->send($msg);
50-
}
51-
if ($isCmd === '1') {
50+
} elseif ($isCmd === '1') {
5251
// print($callTrait->config['client_id']);
5352
$msgData = sendAppButtons($jsMsg);
5453
$msg = new Frame(gzencode(json_encode($msgData)),true,Frame::OP_BINARY);
5554
$conn->send($msg);
5655
}//end of pay1
5756

58-
if ($isCmd === '2') {
57+
elseif ($isCmd === '2') {
5958
// print($callTrait->config['client_id']);
6059
$msgData = sendAppCard($jsMsg);
6160
$msg = new Frame(gzencode(json_encode($msgData)),true,Frame::OP_BINARY);
6261
$conn->send($msg);
6362
}//end of pay2
64-
if ($isCmd === '3') {
63+
elseif ($isCmd === '3') {
6564
transfer();
65+
} else {
66+
$msgData = sendPlainText($jsMsg->data->conversation_id,
67+
base64_decode($jsMsg->data->data));
68+
$msg = new Frame(gzencode(json_encode($msgData)),true,Frame::OP_BINARY);
69+
$conn->send($msg);
6670
}
6771
} //end of PLAIN_TEXT
6872
if ($jsMsg->data->category === 'SYSTEM_ACCOUNT_SNAPSHOT') {
@@ -98,20 +102,25 @@ public function __construct()
98102

99103
$loop->run();
100104

101-
function sendPlainText($jsMsg):Array
102-
{
103-
$msgHelp = <<<HTML
104-
Usage:
105-
? or help : for help!
106-
1 : pay by APP_CARD
107-
2 : pay by APP_BUTTON_GROUP
108-
HTML;
105+
106+
function sendUsage($conversation_id):Array {
107+
$msgHelp = <<<EOF
108+
Usage:
109+
? or help : for help!
110+
1 : pay by APP_CARD
111+
2 : pay by APP_BUTTON_GROUP
112+
EOF;
113+
return sendPlainText($conversation_id,$msgHelp);
114+
}
115+
116+
function sendPlainText($conversation_id,$msgContent):Array {
117+
109118
$msgParams = [
110-
'conversation_id' => $jsMsg->data->conversation_id,
119+
'conversation_id' => $conversation_id,
111120
'category' => 'PLAIN_TEXT',
112121
'status' => 'SENT',
113122
'message_id' => Uuid::uuid4()->toString(),
114-
'data' => base64_encode($msgHelp),//base64_encode("hello!"),
123+
'data' => base64_encode($msgContent),//base64_encode("hello!"),
115124
];
116125
$msgPayButton = [
117126
'id' => Uuid::uuid4()->toString(),

mixin_network-keys.jpg

242 KB
Loading

0 commit comments

Comments
 (0)