在 上一篇教程中, 咱們建立了自動回覆消息的機器人,當用戶發送消息"Hello,World!"時,機器人會自動回覆同一條消息!git
按本篇教程後學習後完成後,你的機器人將會接受用戶發送過來的加密貨幣,而後當即轉回用戶。 完整代碼以下:github
Program.csbash
using System;
using System.Collections.Generic;
using MixinSdk;
using MixinSdk.Bean;
using Newtonsoft.Json;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Generators;
using Org.BouncyCastle.Crypto.Prng;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.X509;
namespace echo_bot
{
class Program
{
static void Main(string[] args) {
MixinApi mixinApi = new MixinApi();
mixinApi.Init(USRCONFIG.ClientId, USRCONFIG.ClientSecret, USRCONFIG.SessionId, USRCONFIG.PinToken, USRCONFIG.PrivateKey);
mixinApi.WebSocketConnect(HandleOnRecivedMessage).Wait();
mixinApi.StartRecive();
do
{
var msg = Console.ReadLine();
} while (true);
}
static void HandleOnRecivedMessage(object sender, EventArgs args, string message) {
var incomingMessage = JsonConvert.DeserializeObject<RecvWebSocketMessage>(message);
Console.WriteLine(incomingMessage);
if ( (incomingMessage.action == "CREATE_MESSAGE") && (incomingMessage.data != null) ) {
// Console.WriteLine(incomingMessage.data.conversation_id);
MixinApi callback = (MixinApi)sender;
//send ack for every Create Message!
callback.SendMessageResponse(incomingMessage.data.message_id).Wait();
if (incomingMessage.data.category == "PLAIN_TEXT") {
byte[] strOriginal = Convert.FromBase64String(incomingMessage.data.data);
string clearText = System.Text.Encoding.UTF8.GetString(strOriginal);
Console.WriteLine(clearText);
string thisMessageId = Guid.NewGuid().ToString();
System.Console.WriteLine("Send echo with message id:" + thisMessageId);
if (clearText == "a") {
AppCard appCard = new AppCard();
appCard.title = "Pay BTC 0.0001";
appCard.icon_url = "https://images.mixin.one/HvYGJsV5TGeZ-X9Ek3FEQohQZ3fE9LBEBGcOcn4c4BNHovP4fW4YB97Dg5LcXoQ1hUjMEgjbl1DPlKg1TW7kK6XP=s128";
appCard.description = "hi";
appCard.action = "https://mixin.one/pay?recipient=" +
USRCONFIG.ClientId + "&asset=" +
"c6d0c728-2624-429b-8e0d-d9d19b6592fa" +
"&amount=" + "0.001" +
"&trace=" + System.Guid.NewGuid().ToString() +
"&memo=";
callback.SendAppCardMessage(incomingMessage.data.conversation_id,appCard);
} else if (clearText == "g") {
List<AppButton> appBtnList = new List<AppButton>();
string payLinkEOS = "https://mixin.one/pay?recipient=" +
USRCONFIG.ClientId + "&asset=" +
"6cfe566e-4aad-470b-8c9a-2fd35b49c68d" +
"&amount=" + "0.1" +
"&trace=" + System.Guid.NewGuid().ToString() +
"&memo=";
string payLinkBTC = "https://mixin.one/pay?recipient=" +
USRCONFIG.ClientId + "&asset=" +
"c6d0c728-2624-429b-8e0d-d9d19b6592fa" +
"&amount=" + "0.001" +
"&trace=" + System.Guid.NewGuid().ToString() +
"&memo=";
AppButton btnBTC = new AppButton();
btnBTC.label = "Pay BTC 0.001";
btnBTC.color = "#0080FF";
btnBTC.action = payLinkBTC;
AppButton btnEOS = new AppButton();
btnEOS.label = "Pay EOS 0.1";
btnEOS.color = "#8000FF";
btnEOS.action = payLinkEOS;
appBtnList.Add(btnBTC);
appBtnList.Add(btnEOS);
callback.SendAppButtonGroupMessage(incomingMessage.data.conversation_id,appBtnList);
} else callback.SendTextMessage(incomingMessage.data.conversation_id, clearText,thisMessageId);
}
if (incomingMessage.data.category == "SYSTEM_ACCOUNT_SNAPSHOT") {
byte[] strOriginal = Convert.FromBase64String(incomingMessage.data.data);
string clearText = System.Text.Encoding.UTF8.GetString(strOriginal);
Console.WriteLine(clearText);
Transfer trsInfo = JsonConvert.DeserializeObject<Transfer>(clearText);
Console.WriteLine(trsInfo.asset_id);
Console.WriteLine(trsInfo.opponent_id);
Console.WriteLine(trsInfo.amount);
if ( Int32.Parse(trsInfo.amount) > 0 ) {
Transfer reqInfo = callback.Transfer(trsInfo.asset_id,
trsInfo.opponent_id,
trsInfo.amount,
USRCONFIG.PinCode,
System.Guid.NewGuid().ToString(),
"");
Console.WriteLine(reqInfo);
}
}
}
// Console.WriteLine(incomingMessage);
if (incomingMessage.action == "ACKNOWLEDGE_MESSAGE_RECEIPT")
{
if (incomingMessage.data != null) {
System.Console.WriteLine("The message delivery status: " +
incomingMessage.data.message_id + " "
+ incomingMessage.data.status);
}
}
if (incomingMessage.action == "LIST_PENDING_MESSAGES")
{
System.Console.WriteLine("The bot is listening!");
}
}
}
}
複製代碼
在項目目錄下編譯並執行app
wenewzha:echo_bot wenewzhang$ dotnet build
Microsoft (R) Build Engine version 15.9.20+g88f5fadfbe for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.
Restore completed in 154.16 ms for /Users/wenewzhang/Documents/sl/mixin_labs-csharp-bot/echo_bot/echo_bot.csproj.
Restore completed in 154.19 ms for /Users/wenewzhang/Documents/sl/mixin_labs-csharp-bot/mixin-csharp-sdk/mixin-csharp-sdk/mixin-csharp-sdk.csproj.
mixin-csharp-sdk -> /Users/wenewzhang/Documents/sl/mixin_labs-csharp-bot/mixin-csharp-sdk/mixin-csharp-sdk/bin/Debug/netstandard2.0/mixin-csharp-sdk.dll
Created package at /Users/wenewzhang/Documents/sl/mixin_labs-csharp-bot/mixin-csharp-sdk/mixin-csharp-sdk/bin/Debug/MixinCSharpSdk.0.1.0.nupkg.
echo_bot -> /Users/wenewzhang/Documents/sl/mixin_labs-csharp-bot/echo_bot/bin/Debug/netcoreapp2.0/echo_bot.dll
Build succeeded.
0 Warning(s)
0 Error(s)
Time Elapsed 00:00:03.33
wenewzha:echo_bot wenewzhang$ dotnet bin/Debug/netcoreapp2.0/echo_bot.dll
{"id":"dd52dfe5-7f12-4c0e-bb44-d1865308e628","action":"LIST_PENDING_MESSAGES","data":null}
The bot is listening!
複製代碼
開發者能夠經過消息面板,給機器人轉比特幣,當機器人收到比特幣後,立刻返還給用戶! 學習
事實上,用戶能夠發送任意的幣種給機器人,它都能立刻返還! ui
if (incomingMessage.data.category == "SYSTEM_ACCOUNT_SNAPSHOT") {
byte[] strOriginal = Convert.FromBase64String(incomingMessage.data.data);
string clearText = System.Text.Encoding.UTF8.GetString(strOriginal);
Transfer trsInfo = JsonConvert.DeserializeObject<Transfer>(clearText);
if ( Int32.Parse(trsInfo.amount) > 0 ) {
Transfer reqInfo = callback.Transfer(trsInfo.asset_id,
trsInfo.opponent_id,
trsInfo.amount,
USRCONFIG.PinCode,
System.Guid.NewGuid().ToString(),
"");
Console.WriteLine(reqInfo);
}
複製代碼
若是機器人收到幣,trsInfo.amount 大於零;若是機器人支付幣給用戶,接收到的消息是同樣的,惟一不一樣的是,trsInfo.amount是一個負數. 最後一步,調用SDK的 callback.Transfer 將幣返還用戶!this
在一些應用場景,好比:有一個交易所想提供換幣服務,將比特幣換成以太坊,EOS,比特幣現金等, 你想顯示給用戶一組按鈕,它們分別表明不一樣的幣與不一樣的數量,APP_BUTTON_GROUP能夠幫你作到這一點.加密
List<AppButton> appBtnList = new List<AppButton>();
string payLinkEOS = "https://mixin.one/pay?recipient=" +
USRCONFIG.ClientId + "&asset=" +
"6cfe566e-4aad-470b-8c9a-2fd35b49c68d" +
"&amount=" + "0.1" +
"&trace=" + System.Guid.NewGuid().ToString() +
"&memo=";
string payLinkBTC = "https://mixin.one/pay?recipient=" +
USRCONFIG.ClientId + "&asset=" +
"c6d0c728-2624-429b-8e0d-d9d19b6592fa" +
"&amount=" + "0.001" +
"&trace=" + System.Guid.NewGuid().ToString() +
"&memo=";
AppButton btnBTC = new AppButton();
btnBTC.label = "Pay BTC 0.001";
btnBTC.color = "#0080FF";
btnBTC.action = payLinkBTC;
AppButton btnEOS = new AppButton();
btnEOS.label = "Pay EOS 0.1";
btnEOS.color = "#8000FF";
btnEOS.action = payLinkEOS;
appBtnList.Add(btnBTC);
appBtnList.Add(btnEOS);
callback.SendAppButtonGroupMessage(incomingMessage.data.conversation_id,appBtnList);
複製代碼
這裏演示給用戶BTC與EOS兩種,你還能夠增長更多的按鈕.url
若是你以爲一組按鈕太單調了,能夠試一下APP_CARD,它提供一個圖標的連接spa
AppCard appCard = new AppCard();
appCard.title = "Pay BTC 0.0001";
appCard.icon_url = "https://images.mixin.one/HvYGJsV5TGeZ-X9Ek3FEQohQZ3fE9LBEBGcOcn4c4BNHovP4fW4YB97Dg5LcXoQ1hUjMEgjbl1DPlKg1TW7kK6XP=s128";
appCard.description = "hi";
appCard.action = "https://mixin.one/pay?recipient=" +
USRCONFIG.ClientId + "&asset=" +
"c6d0c728-2624-429b-8e0d-d9d19b6592fa" +
"&amount=" + "0.001" +
"&trace=" + System.Guid.NewGuid().ToString() +
"&memo=";
callback.SendAppCardMessage(incomingMessage.data.conversation_id,appCard);
複製代碼
Full code is here