In the last tutorial, we created a bot that automatically replies to messages when a user sends a message “Hello,World!” “, the robot will automatically reply to the same message!
Lesson two: The robot accepts bitcoin and immediately returns it to the user
After completing this tutorial, your bot will accept cryptocurrency sent by the user and then immediately return to the user. In app.java, find the switch condition and add a new “CASE” :
App.java
switch (category) {
...
case SYSTEM_ACCOUNT_SNAPSHOT:
userId =
obj.get("data").getAsJsonObject().get("user_id").getAsString();
byte[] JsData = Base64.decodeBase64(obj.get("data").getAsJsonObject().get("data").getAsString());
String JsStr = new String(JsData);
System.out.println("SYSTEM_ACCOUNT_SNAPSHOT json: " + JsStr);
JsonObject jsObj = new JsonParser().parse(JsStr).getAsJsonObject();
System.out.println(jsObj.get("amount").getAsString());
System.out.println(jsObj.get("asset_id").getAsString());
if (jsObj.get("amount").getAsFloat() > 0) {
String aesKey = new String (Base64.encodeBase64(Config.PAY_KEY));
System.out.println(aesKey);
System.out.println(Config.PAY_KEY.length);
String encryptPin = MixinUtil.encryptPayKey(Config.PIN,Config.PAY_KEY);
MixinBot.transfer(
jsObj.get("asset_id").getAsString(),
jsObj.get("opponent_id").getAsString(),
jsObj.get("amount").getAsString(),
encryptPin,
Config.RSA_PRIVATE_KEY,
Config.CLIENT_ID,
Config.SESSION_ID
);
}
break; .Copy the code
Hello Bitcoin!
In the project directory, execute gradle run **
Response: response {protocol= HTTP /1.1, code=101, message=Switching Protocols, url=https://blaze.mixin.one/} [onMessage!! ] json: {"id":"712fbde1-1b72-4e8f-b731-b7dc6f689c3e"."action":"LIST_PENDING_MESSAGES"}
LIST_PENDING_MESSAGES
<=========----> 75% EXECUTING [13s]
> :run
Copy the code
Developers can transfer bitcoins to the robot through the message panel, and when the robot receives the bitcoins, it will be returned to the user immediately!
In fact, users can send any currency to the robot and it will be returned immediately!
Source code interpretation
if (jsObj.get("amount").getAsFloat() > 0) {
String encryptPin = MixinUtil.encryptPayKey(Config.PIN,Config.PAY_KEY);
MixinBot.transfer(
jsObj.get("asset_id").getAsString(),
jsObj.get("opponent_id").getAsString(),
jsObj.get("amount").getAsString(),
encryptPin,
Config.RSA_PRIVATE_KEY,
Config.CLIENT_ID,
Config.SESSION_ID
);
}
Copy the code
If the robot receives coins, jsobj.get (“amount”) is greater than zero; If the bot pays the user with coins, it receives the same message, except that jsobj.get (“amount”) is a negative number. Finally, call mixinbot. transfer of SDK to return coins to users!