摘要:如果機(jī)器人收到幣,大于零如果機(jī)器人支付幣給用戶,接收到的消息是一樣的,唯一不同的是是一個(gè)負(fù)數(shù)最后一步,調(diào)用將幣還給用戶完整的代碼在這兒
在 上一篇教程中, 我們創(chuàng)建了自動(dòng)回復(fù)消息的機(jī)器人,當(dāng)用戶發(fā)送消息"Hello,World!"時(shí),機(jī)器人會(huì)自動(dòng)回復(fù)同一條消息!
按本篇教程后學(xué)習(xí)后完成后,你的機(jī)器人將會(huì)接受用戶發(fā)送過來的加密貨幣,然后立即轉(zhuǎn)回用戶。下面是全部源代碼,創(chuàng)建一個(gè)app.php試一下吧!
15 ]); class callTraitClass { use MixinSDKTrait; public $config; public function __construct() { $config = require(__DIR__."/config.php"); $this->config = $config; } } $callTrait = new callTraitClass(); $Token = $callTrait->getToken("GET", "/", ""); print_r($callTrait->config["client_id"]); // $Header = "Authorization"."Bearer ".$Token; // print($Header); $connector = new RatchetClientConnector($loop,$reactConnector); // $connector("ws://127.0.0.1:9000", ["protocol" => "Mixin-Blaze-1"], ["Origin" => "http://localhost", $connector("wss://blaze.mixin.one", ["protocol" => "Mixin-Blaze-1"],[ "Authorization" => "Bearer ".$Token ]) ->then(function(RatchetClientWebSocket $conn) { $conn->on("message", function(RatchetRFC6455MessagingMessageInterface $msg) use ($conn) { $jsMsg = json_decode(gzdecode($msg)); print_r($jsMsg); if ($jsMsg->action === "CREATE_MESSAGE" and property_exists($jsMsg,"data")) { echo " Need reply server a receipt! "; $RspMsg = generateReceipt($jsMsg->data->message_id); $msg = new Frame(gzencode(json_encode($RspMsg)),true,Frame::OP_BINARY); $conn->send($msg); if ($jsMsg->data->category === "PLAIN_TEXT") { echo "PLAIN_TEXT:".base64_decode($jsMsg->data->data); $isCmd = strtolower(base64_decode($jsMsg->data->data)); if ($isCmd ==="?" or $isCmd ==="help") { $msgData = sendUsage($jsMsg->data->conversation_id); $msg = new Frame(gzencode(json_encode($msgData)),true,Frame::OP_BINARY); $conn->send($msg); } elseif ($isCmd === "1") { // print($callTrait->config["client_id"]); $msgData = sendAppButtons($jsMsg); $msg = new Frame(gzencode(json_encode($msgData)),true,Frame::OP_BINARY); $conn->send($msg); }//end of pay1 elseif ($isCmd === "2") { // print($callTrait->config["client_id"]); $msgData = sendAppCard($jsMsg); $msg = new Frame(gzencode(json_encode($msgData)),true,Frame::OP_BINARY); $conn->send($msg); }//end of pay2 elseif ($isCmd === "3") { transfer(); } else { $msgData = sendPlainText($jsMsg->data->conversation_id, base64_decode($jsMsg->data->data)); $msg = new Frame(gzencode(json_encode($msgData)),true,Frame::OP_BINARY); $conn->send($msg); } } //end of PLAIN_TEXT if ($jsMsg->data->category === "SYSTEM_ACCOUNT_SNAPSHOT") { // refundInstant echo "user id:".$jsMsg->data->user_id; $dtPay = json_decode(base64_decode($jsMsg->data->data)); print_r($dtPay); if ($dtPay->amount > 0) { echo "paid!".$dtPay->asset_id; refundInstant($dtPay->asset_id,$dtPay->amount,$jsMsg->data->user_id); } } //end of SYSTEM_ACCOUNT_SNAPSHOT } //end of CREATE_MESSAGE }); $conn->on("close", function($code = null, $reason = null) { echo "Connection closed ({$code} - {$reason}) "; }); /* start listen for the incoming message */ $message = [ "id" => Uuid::uuid4()->toString(), "action" => "LIST_PENDING_MESSAGES", ]; print_r(json_encode($message)); $msg = new Frame(gzencode(json_encode($message)),true,Frame::OP_BINARY); $conn->send($msg); // $conn->send(gzencode($msg,1,FORCE_DEFLATE)); }, function(Exception $e) use ($loop) { echo "Could not connect: {$e->getMessage()} "; $loop->stop(); }); $loop->run(); function sendUsage($conversation_id):Array { $msgHelp = <<你好,我的幣!$conversation_id, "category" => "PLAIN_TEXT", "status" => "SENT", "message_id" => Uuid::uuid4()->toString(), "data" => base64_encode($msgContent),//base64_encode("hello!"), ]; $msgPayButton = [ "id" => Uuid::uuid4()->toString(), "action" => "CREATE_MESSAGE", "params" => $msgParams, ]; return $msgPayButton; } function sendAppButtons($jsMsg):Array { $payLinkEOS = "https://mixin.one/pay?recipient=". "a1ce2967-a534-417d-bf12-c86571e4eefa"."&asset=". "6cfe566e-4aad-470b-8c9a-2fd35b49c68d". "&amount=0.0001"."&trace=".Uuid::uuid4()->toString(). "&memo="; $payLinkBTC = "https://mixin.one/pay?recipient=". "a1ce2967-a534-417d-bf12-c86571e4eefa"."&asset=". "c6d0c728-2624-429b-8e0d-d9d19b6592fa". "&amount=0.0001"."&trace=".Uuid::uuid4()->toString(). "&memo="; $msgData = [[ "label" => "Pay 0.001 EOS", "color" => "#FFABAB", "action" => $payLinkEOS, ],[ "label" => "Pay 0.0001 BTC", "color" => "#00EEFF", "action" => $payLinkBTC, ], ]; $msgParams = [ "conversation_id" => $jsMsg->data->conversation_id,// $callTrait->config[client_id], // "recipient_id" => $jsMsg->data->user_id, "category" => "APP_BUTTON_GROUP",//"PLAIN_TEXT", "status" => "SENT", "message_id" => Uuid::uuid4()->toString(), "data" => base64_encode(json_encode($msgData)),//base64_encode("hello!"), ]; $msgPayButtons = [ "id" => Uuid::uuid4()->toString(), "action" => "CREATE_MESSAGE", "params" => $msgParams, ]; return $msgPayButtons; } function sendAppCard($jsMsg):Array { $payLink = "https://mixin.one/pay?recipient=". "a1ce2967-a534-417d-bf12-c86571e4eefa"."&asset=". "6cfe566e-4aad-470b-8c9a-2fd35b49c68d". "&amount=0.0001"."&trace=".Uuid::uuid4()->toString(). "&memo="; $msgData = [ "icon_url" => "https://mixin.one/assets/98b586edb270556d1972112bd7985e9e.png", "title" => "Pay 0.001 EOS", "description" => "pay", "action" => $payLink, ]; $msgParams = [ "conversation_id" => $jsMsg->data->conversation_id,// $callTrait->config[client_id], // "recipient_id" => $jsMsg->data->user_id, "category" => "APP_CARD",//"PLAIN_TEXT", "status" => "SENT", "message_id" => Uuid::uuid4()->toString(), "data" => base64_encode(json_encode($msgData)),//base64_encode("hello!"), ]; $msgPayButton = [ "id" => Uuid::uuid4()->toString(), "action" => "CREATE_MESSAGE", "params" => $msgParams, ]; return $msgPayButton; } function transfer() { $mixinSdk = new MixinSDK(require "./config.php"); print_r($mixinSdk->getConfig()); } function generateReceipt($msgID):Array { $IncomingMsg = ["message_id" => $msgID, "status" => "READ"]; $RspMsg = ["id" => Uuid::uuid4()->toString(), "action" => "ACKNOWLEDGE_MESSAGE_RECEIPT", "params" => $IncomingMsg]; return $RspMsg; } function refundInstant($_assetID,$_amount,$_opponent_id) { $mixinSdk = new MixinSDK(require "./config.php"); // print_r(); $BotInfo = $mixinSdk->Wallet()->transfer($_assetID,$_opponent_id, $mixinSdk->getConfig()["default"]["pin"],$_amount); print_r($BotInfo); }
在工程目錄下,執(zhí)行 php app.php
php app.php
wenewzha:mixin_labs-php-bot wenewzhang$ php app.php a1ce2967-a534-417d-bf12-c86571e4eefa{"id":"12c7a470-d6a4-403d-94e8-e6f8ae833971","action":"LIST_PENDING_MESSAGES"}stdClass Object ( [id] => 12c7a470-d6a4-403d-94e8-e6f8ae833971 [action] => LIST_PENDING_MESSAGES )
如果控制臺(tái)出現(xiàn) "LIST_PENDING_MESSAGES"字樣, 連接到mixin.one成功了,正在偵聽用戶發(fā)送消息給它!
按幫助來操作,發(fā)送消息得到相應(yīng)的支付提示
1 機(jī)器人回復(fù) APP_CARD 支付鏈接.
2 機(jī)器人回復(fù) APP_BUTTON_GROUP 支付鏈接.
? or help : 顯示幫助
點(diǎn)擊上面的鏈接,將會(huì)彈出一個(gè)窗口,輸入你的密碼,將支付幣給機(jī)器人!
Mixin Messenger支持的消息類型
如下圖所示,用戶點(diǎn)擊支付鏈接,輸入密碼,支付0.01 EOS給機(jī)器人,機(jī)器人馬上返還給用戶!
親愛的開發(fā)者,你也可以從消息控制面板里,點(diǎn)擊轉(zhuǎn)帳,直接將幣轉(zhuǎn)給機(jī)器人!它還是一樣的立即返還!
$msg = new Frame(gzencode(json_encode($msgData)),true,Frame::OP_BINARY); $conn->send($msg);
機(jī)器人發(fā)送給用戶的消息,先用json序列化,再用gzencode壓縮。
if ($jsMsg->data->category === "SYSTEM_ACCOUNT_SNAPSHOT") { // refundInstant echo "user id:".$jsMsg->data->user_id; $dtPay = json_decode(base64_decode($jsMsg->data->data)); print_r($dtPay); if ($dtPay->amount > 0) { echo "paid!".$dtPay->asset_id; refundInstant($dtPay->asset_id,$dtPay->amount,$jsMsg->data->user_id); } } //end of SYSTEM_ACCOUNT_SNAPSHOT
如果機(jī)器人收到幣,
$dtPay->amount
大于零;如果機(jī)器人支付幣給用戶,接收到的消息是一樣的,唯一不同的是
$dtPay->amount
是一個(gè)負(fù)數(shù).
function refundInstant($_assetID,$_amount,$_opponent_id) { $mixinSdk = new MixinSDK(require "./config.php"); // print_r(); $BotInfo = $mixinSdk->Wallet()->transfer($_assetID,$_opponent_id, $mixinSdk->getConfig()["default"]["pin"],$_amount); print_r($BotInfo); }
最后一步,調(diào)用MixinSDK將幣還給用戶!
完整的代碼在這兒 here
**
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://specialneedsforspecialkids.com/yun/30957.html
摘要:如果機(jī)器人收到幣,大于零如果機(jī)器人支付幣給用戶,接收到的消息是一樣的,唯一不同的是是一個(gè)負(fù)數(shù)最后一步,調(diào)用將幣還給用戶完整的代碼在這兒 showImg(https://segmentfault.com/img/remote/1460000018419614?w=1200&h=659); 在 上一篇教程中, 我們創(chuàng)建了自動(dòng)回復(fù)消息的機(jī)器人,當(dāng)用戶發(fā)送消息Hello,World!時(shí),機(jī)器人...
摘要:在上一篇教程中我們創(chuàng)建了自動(dòng)回復(fù)消息的機(jī)器人當(dāng)用戶發(fā)送消息時(shí),機(jī)器人會(huì)自動(dòng)回復(fù)同一條消息按本篇教程后學(xué)習(xí)后完成后,你的機(jī)器人將會(huì)接受用戶發(fā)送過來的加密貨幣,然后立即轉(zhuǎn)回用戶。 showImg(https://segmentfault.com/img/remote/1460000019234638?w=1200&h=659); 在 上一篇教程中, 我們創(chuàng)建了自動(dòng)回復(fù)消息的機(jī)器人,當(dāng)用戶發(fā)...
摘要:英文原文是一個(gè)免費(fèi)的極速的端對(duì)端加密數(shù)字貨幣交易系統(tǒng)在本章中,你可以按教程在中創(chuàng)建一個(gè)來接收用戶消息學(xué)到如何給機(jī)器人轉(zhuǎn)比特幣或者讓機(jī)器人給你轉(zhuǎn)比特幣資源匯總課程簡(jiǎn)介創(chuàng)建一個(gè)接受消息的機(jī)器人機(jī)器人接受比特幣并立即退還用戶如何通過創(chuàng)建新帳號(hào)創(chuàng)建 英文原文 Mixin Network 是一個(gè)免費(fèi)的 極速的端對(duì)端加密數(shù)字貨幣交易系統(tǒng).在本章中,你可以按教程在Mixin Messenger中創(chuàng)建...
摘要:英文原文是一個(gè)免費(fèi)的極速的端對(duì)端加密數(shù)字貨幣交易系統(tǒng)在本章中,你可以按教程在中創(chuàng)建一個(gè)來接收用戶消息學(xué)到如何給機(jī)器人轉(zhuǎn)比特幣或者讓機(jī)器人給你轉(zhuǎn)比特幣資源匯總課程簡(jiǎn)介創(chuàng)建一個(gè)接受消息的機(jī)器人機(jī)器人接受比特幣并立即退還用戶如何通過創(chuàng)建新帳號(hào)創(chuàng)建 英文原文 Mixin Network 是一個(gè)免費(fèi)的 極速的端對(duì)端加密數(shù)字貨幣交易系統(tǒng).在本章中,你可以按教程在Mixin Messenger中創(chuàng)建...
摘要:比特幣的到帳時(shí)間是個(gè)區(qū)塊的高度,約分鐘。請(qǐng)注意,比特幣與的地址是一樣的。 showImg(https://segmentfault.com/img/remote/1460000018378805?w=1200&h=659); 方案一: 通過ExinCore API進(jìn)行幣幣交易 Exincore 提供了基于Mixin Network的幣幣交易API. 你可以支付USDT給ExinCore...
閱讀 2791·2021-11-17 09:33
閱讀 4454·2021-09-22 15:57
閱讀 2866·2019-08-30 14:16
閱讀 3133·2019-08-29 14:07
閱讀 2413·2019-08-26 11:55
閱讀 3415·2019-08-23 17:07
閱讀 1725·2019-08-23 16:50
閱讀 2535·2019-08-23 16:08