摘要:如果機器人收到幣,大于零如果機器人支付幣給用戶,接收到的消息是一樣的,唯一不同的是是一個負數最后一步,調用將幣還給用戶完整的代碼在這兒
在 上一篇教程中, 我們創建了自動回復消息的機器人,當用戶發送消息"Hello,World!"時,機器人會自動回復同一條消息!
按本篇教程后學習后完成后,你的機器人將會接受用戶發送過來的加密貨幣,然后立即轉回用戶。下面是全部源代碼,創建一個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); }
在工程目錄下,執行 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 )
如果控制臺出現 "LIST_PENDING_MESSAGES"字樣, 連接到mixin.one成功了,正在偵聽用戶發送消息給它!
按幫助來操作,發送消息得到相應的支付提示
1 機器人回復 APP_CARD 支付鏈接.
2 機器人回復 APP_BUTTON_GROUP 支付鏈接.
? or help : 顯示幫助
點擊上面的鏈接,將會彈出一個窗口,輸入你的密碼,將支付幣給機器人!
Mixin Messenger支持的消息類型
如下圖所示,用戶點擊支付鏈接,輸入密碼,支付0.01 EOS給機器人,機器人馬上返還給用戶!
親愛的開發者,你也可以從消息控制面板里,點擊轉帳,直接將幣轉給機器人!它還是一樣的立即返還!
$msg = new Frame(gzencode(json_encode($msgData)),true,Frame::OP_BINARY); $conn->send($msg);
機器人發送給用戶的消息,先用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
如果機器人收到幣,
$dtPay->amount
大于零;如果機器人支付幣給用戶,接收到的消息是一樣的,唯一不同的是
$dtPay->amount
是一個負數.
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); }
最后一步,調用MixinSDK將幣還給用戶!
完整的代碼在這兒 here
**
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/24590.html
摘要:如果機器人收到幣,大于零如果機器人支付幣給用戶,接收到的消息是一樣的,唯一不同的是是一個負數最后一步,調用將幣還給用戶完整的代碼在這兒 showImg(https://segmentfault.com/img/remote/1460000018419614?w=1200&h=659); 在 上一篇教程中, 我們創建了自動回復消息的機器人,當用戶發送消息Hello,World!時,機器人...
摘要:在上一篇教程中我們創建了自動回復消息的機器人當用戶發送消息時,機器人會自動回復同一條消息按本篇教程后學習后完成后,你的機器人將會接受用戶發送過來的加密貨幣,然后立即轉回用戶。 showImg(https://segmentfault.com/img/remote/1460000019234638?w=1200&h=659); 在 上一篇教程中, 我們創建了自動回復消息的機器人,當用戶發...
摘要:英文原文是一個免費的極速的端對端加密數字貨幣交易系統在本章中,你可以按教程在中創建一個來接收用戶消息學到如何給機器人轉比特幣或者讓機器人給你轉比特幣資源匯總課程簡介創建一個接受消息的機器人機器人接受比特幣并立即退還用戶如何通過創建新帳號創建 英文原文 Mixin Network 是一個免費的 極速的端對端加密數字貨幣交易系統.在本章中,你可以按教程在Mixin Messenger中創建...
摘要:英文原文是一個免費的極速的端對端加密數字貨幣交易系統在本章中,你可以按教程在中創建一個來接收用戶消息學到如何給機器人轉比特幣或者讓機器人給你轉比特幣資源匯總課程簡介創建一個接受消息的機器人機器人接受比特幣并立即退還用戶如何通過創建新帳號創建 英文原文 Mixin Network 是一個免費的 極速的端對端加密數字貨幣交易系統.在本章中,你可以按教程在Mixin Messenger中創建...
摘要:比特幣的到帳時間是個區塊的高度,約分鐘。請注意,比特幣與的地址是一樣的。 showImg(https://segmentfault.com/img/remote/1460000018378805?w=1200&h=659); 方案一: 通過ExinCore API進行幣幣交易 Exincore 提供了基于Mixin Network的幣幣交易API. 你可以支付USDT給ExinCore...
閱讀 3924·2021-10-12 10:12
閱讀 2886·2021-09-10 11:18
閱讀 3672·2019-08-30 15:54
閱讀 2808·2019-08-30 15:53
閱讀 638·2019-08-30 13:54
閱讀 965·2019-08-30 13:21
閱讀 2259·2019-08-30 12:57
閱讀 1686·2019-08-30 11:10