摘要:在上一篇教程中我们创建了自动回复消息的机器人当用户发送消息时,机器人会自动回复同一条消息按本篇教程后学习后完成后,你的机器人将会接受用户发送过来的加密货币,然后立即转回用户。
在 上一篇教程中, 我们创建了自动回复消息的机器人,当用户发送消息"Hello,World!"时,机器人会自动回复同一条消息!
按本篇教程后学习后完成后,你的机器人将会接受用户发送过来的加密货币,然后立即转回用户。下面是全部源代码,创建一个websocket_mixin_bot.rb试一下吧!
require "faye/websocket" require "eventmachine" require "json" require "zlib" require "mixin_bot" require "yaml" yaml_hash = YAML.load_file("./config.yml") MixinBot.client_id = yaml_hash["MIXIN_CLIENT_ID"] MixinBot.session_id = yaml_hash["MIXIN_SESSION_ID"] MixinBot.client_secret = yaml_hash["MIXIN_CLIENT_SECRET"] MixinBot.pin_token = yaml_hash["MIXIN_PIN_TOKEN"] MixinBot.private_key = yaml_hash["MIXIN_PRIVATE_KEY"] access_token = MixinBot.api.access_token("GET","/","") puts access_token authorization = format("Bearer %s", access_token) EM.run { ws = Faye::WebSocket::Client.new("wss://blaze.mixin.one/", ["Mixin-Blaze-1"], :headers => { "Authorization" => authorization } ) ws.on :open do |event| p [:open] ws.send(MixinBot.api.list_pending_message) end ws.on :message do |event| p [:message] data = event.data msg = MixinBot.api.read_message(data) jsmsg = JSON.parse msg p jsmsg # p jsmsg["data"] if jsmsg["action"] == "CREATE_MESSAGE" && jsmsg["data"] != nil msgid = jsmsg["data"]["message_id"] ws.send(MixinBot.api.acknowledge_message_receipt(msgid)) if jsmsg["data"]["category"] == "PLAIN_TEXT" conversation_id = jsmsg["data"]["conversation_id"] decoded_msg = Base64.decode64 jsmsg["data"]["data"] p decoded_msg if decoded_msg == "?" or decoded_msg == "h" or decoded_msg == "H" reply_msg = "?: help " + "1: Payment link for APP_CARD " + "2: Payment link for APP_BUTTON_GROUP " reply_msg = MixinBot.api.plain_text_message(conversation_id,reply_msg) ws.send(reply_msg) elsif (decoded_msg == "1") payLinkEOS = "https://mixin.one/pay?recipient=" + "a1ce2967-a534-417d-bf12-c86571e4eefa" + "&asset=" + "6cfe566e-4aad-470b-8c9a-2fd35b49c68d" + "&amount=0.001" + "&trace=" + SecureRandom.uuid + "&memo=" msgData = { "icon_url":"https://mixin.one/assets/98b586edb270556d1972112bd7985e9e.png", "title":"Pay 0.001 EOS", "description":"pay", "action":payLinkEOS } ws.send(MixinBot.api. app_card_message(conversation_id, msgData)) elsif decoded_msg == "2" payLinkEOS = "https://mixin.one/pay?recipient=" + "a1ce2967-a534-417d-bf12-c86571e4eefa" + "&asset=" + "6cfe566e-4aad-470b-8c9a-2fd35b49c68d" + "&amount=0.001" + "&trace=" + SecureRandom.uuid + "&memo=" payLinkEOS = "https://mixin.one/pay?recipient=" + MixinBot.client_id + "&asset=" + "6cfe566e-4aad-470b-8c9a-2fd35b49c68d" + "&amount=0.001" + "&trace=" + SecureRandom.uuid + "&memo=" payLinkBTC = "https://mixin.one/pay?recipient=" + MixinBot.client_id + "&asset=" + "c6d0c728-2624-429b-8e0d-d9d19b6592fa" + "&amount=0.0001" + "&trace=" + SecureRandom.uuid + "&memo=" button1 = { label: "Pay 0.001 EOS", color: "#FFABAB", action: payLinkEOS } button2 = { label: "Pay 0.0001 BTC", color: "#00EEFF", action: payLinkBTC } buttons = [button1,button2] ws.send(MixinBot.api. app_button_group_message(conversation_id, jsmsg["data"]["user_id"], buttons)) p "send app button group" else reply_msg = MixinBot.api.plain_text_message(conversation_id,decoded_msg) ws.send(reply_msg) end end if jsmsg["data"]["category"] == "SYSTEM_ACCOUNT_SNAPSHOT" jsdata = JSON.parse (Base64.decode64(jsmsg["data"]["data"])) p jsdata["amount"] if jsdata["amount"].to_f > 0 p "The Bot got coins:" + jsdata["amount"] transInfo = MixinBot.api.create_transfer(MixinBot.api.encrypt_pin(yaml_hash["MIXIN_PIN_CODE"]), { asset_id: jsdata["asset_id"], opponent_id: jsdata["opponent_id"], amount: jsdata["amount"], trace_id: SecureRandom.uuid, memo: "from ruby" }) p transInfo end end end end ws.on :error do |event| p [:error] end ws.on :close do |event| p [:close, event.code, event.reason] ws = nil end }你好,我的币!
在工程目录下,执行 ruby websocket_mixin_bot.rb
ruby websocket_mixin_bot.rb
wenewzha:mixin_labs-ruby-bot wenewzhang$ ruby websocket_mixin_bot.rb [:open] [:message] {"id"=>"1003b2b5-b8e4-4b54-bd03-983c61f64869", "action"=>"LIST_PENDING_MESSAGES"}
如果控制台出现 "LIST_PENDING_MESSAGES"字样, 连接到mixin.one成功了,正在侦听用户发送消息给它!
按帮助来操作,发送消息得到相应的支付提示
1 机器人回复 APP_CARD 支付链接.
2 机器人回复 APP_BUTTON_GROUP 支付链接.
? or help : 显示帮助
点击上面的链接,将会弹出一个窗口,输入你的密码,将支付币给机器人!
Mixin Messenger支持的消息类型
如下图所示,用户点击支付链接,输入密码,支付0.01 EOS给机器人,机器人马上返还给用户!
亲爱的开发者,你也可以从消息控制面板里,点击转帐,直接将币转给机器人!它还是一样的立即返还!
if jsmsg["data"]["category"] == "SYSTEM_ACCOUNT_SNAPSHOT" jsdata = JSON.parse (Base64.decode64(jsmsg["data"]["data"])) p jsdata["amount"] if jsdata["amount"].to_f > 0 p "The Bot got coins:" + jsdata["amount"] transInfo = MixinBot.api.create_transfer(MixinBot.api.encrypt_pin(yaml_hash["MIXIN_PIN_CODE"]), { asset_id: jsdata["asset_id"], opponent_id: jsdata["opponent_id"], amount: jsdata["amount"], trace_id: SecureRandom.uuid, memo: "from ruby" }) p transInfo end end
如果机器人收到币,
jsdata["amount"]
大于零;如果机器人支付币给用户,接收到的消息是一样的,唯一不同的是
jsdata["amount"]
是一个负数.
transInfo = MixinBot.api.create_transfer(MixinBot.api.encrypt_pin(yaml_hash["MIXIN_PIN_CODE"]), { asset_id: jsdata["asset_id"], opponent_id: jsdata["opponent_id"], amount: jsdata["amount"], trace_id: SecureRandom.uuid, memo: "from ruby" }) p transInfo
最后一步,调用MixinSDK将币还给用户!
完整的代码在这儿 here
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/24674.html
摘要:如果机器人收到币,大于零如果机器人支付币给用户,接收到的消息是一样的,唯一不同的是是一个负数最后一步,调用将币还给用户完整的代码在这儿 showImg(https://segmentfault.com/img/remote/1460000018419614?w=1200&h=659); 在 上一篇教程中, 我们创建了自动回复消息的机器人,当用户发送消息Hello,World!时,机器人...
摘要:如果机器人收到币,大于零如果机器人支付币给用户,接收到的消息是一样的,唯一不同的是是一个负数最后一步,调用将币还给用户完整的代码在这儿 showImg(https://segmentfault.com/img/remote/1460000018419614?w=1200&h=659); 在 上一篇教程中, 我们创建了自动回复消息的机器人,当用户发送消息Hello,World!时,机器人...
摘要:是一个免费的极速的端对端加密数字货币交易系统官方资源汇总课程简介创建一个机器人比特币开发教程机器人接受比特币并立即退还用户比特币开发教程创建比特币钱包买卖实时兑换买卖在自由市场挂单买卖买卖任意在自由市场挂单买卖其他编程语言汇总比特币开发教程 showImg(https://segmentfault.com/img/remote/1460000019234638?w=1200&h=659...
摘要:课程简介创建一个机器人机器人接受比特币并立即退还用户创建比特币钱包买卖实时兑换买卖在自由市场挂单买卖买卖任意在自由市场挂单买卖其他编程语言比特币开发教程如下比特币开发教程比特币开发教程比特币开发教程比特币开发教程比特币开发教程比特币开发教程 showImg(https://segmentfault.com/img/bVboFQ6?w=1200&h=659); 课程简介 PHP 创建一...
摘要:课程简介创建一个机器人机器人接受比特币并立即退还用户创建比特币钱包买卖实时兑换买卖在自由市场挂单买卖买卖任意在自由市场挂单买卖其他编程语言比特币开发教程如下比特币开发教程比特币开发教程比特币开发教程比特币开发教程比特币开发教程比特币开发教程 showImg(https://segmentfault.com/img/bVboFQ6?w=1200&h=659); 课程简介 PHP 创建一...
阅读 1648·2021-11-15 11:38
阅读 4464·2021-09-22 15:33
阅读 2297·2021-08-30 09:46
阅读 2147·2019-08-30 15:43
阅读 812·2019-08-30 14:16
阅读 2050·2019-08-30 13:09
阅读 1239·2019-08-30 11:25
阅读 683·2019-08-29 16:42