def on_message(ws, message):
    inbuffer = BytesIO(message)

    f = gzip.GzipFile(mode="rb", fileobj=inbuffer)
    rdata_injson = f.read()
    rdata_obj = json.loads(rdata_injson)
    print("-------json object begin---------")
    print(rdata_obj)
    print("-------json object end---------")
    action = rdata_obj["action"]

    if rdata_obj["data"] is not None:
        print("data in message:", rdata_obj["data"])

    if rdata_obj["data"] is not None and rdata_obj["data"][
            "category"] is not None:
        print(rdata_obj["data"]["category"])

    if action == "CREATE_MESSAGE":

        data = rdata_obj["data"]
        msgid = data["message_id"]
        typeindata = data["type"]
        categoryindata = data["category"]
        userId = data["user_id"]
        conversationId = data["conversation_id"]
        dataindata = data["data"]

        realData = base64.b64decode(dataindata)

        MIXIN_WS_API.replayMessage(ws, msgid)

        if 'error' in rdata_obj:
            return

        if categoryindata == "PLAIN_TEXT":
            realData = realData.decode('utf-8')
            print("dataindata", realData)
            MIXIN_WS_API.sendUserText(ws, conversationId, userId, realData)
def on_message(ws, message):
    inbuffer = BytesIO(message)

    f = gzip.GzipFile(mode="rb", fileobj=inbuffer)
    rdata_injson = f.read()
    rdata_obj = json.loads(rdata_injson)
    print("-------json object begin---------")
    print(rdata_obj)
    print("-------json object end---------")
    action = rdata_obj["action"]
    if action == "ERROR":
        return

    # if rdata_obj["data"] is not None:
    #     print("data in message:",rdata_obj["data"])
    #
    # if rdata_obj["data"] is not None and rdata_obj["data"]["category"] is not None:
    #     print(rdata_obj["data"]["category"])

    if action == "CREATE_MESSAGE":

        data = rdata_obj["data"]
        msgid = data["message_id"]
        typeindata = data["type"]
        categoryindata = data["category"]
        userId = data["user_id"]
        conversationId = data["conversation_id"]
        dataindata = data["data"]
        created_at = data["created_at"]
        updated_at = data["updated_at"]

        realData = base64.b64decode(dataindata)

        MIXIN_WS_API.replayMessage(ws, msgid)

        print('userId', userId)
        print("created_at", created_at)

        if categoryindata == "PLAIN_TEXT":
            realData = realData.decode('utf-8')
            print("dataindata", realData)
            if 'a' == realData:
                print('send a link APP_CARD')
                MIXIN_WS_API.sendAppCard(
                    ws, conversationId, mixin_config.client_id, BTC_ASSET_ID,
                    "0.0001",
                    "https://images.mixin.one/HvYGJsV5TGeZ-X9Ek3FEQohQZ3fE9LBEBGcOcn4c4BNHovP4fW4YB97Dg5LcXoQ1hUjMEgjbl1DPlKg1TW7kK6XP=s128",
                    "Pay BTC 0.0001", "topay")
                return
            if 'g' == realData:
                print('send a link APP_BUTTON_GROUP')
                btnBTC = MIXIN_WS_API.packButton(mixin_config.client_id,
                                                 BTC_ASSET_ID, "0.0001",
                                                 "BTC pay")
                btnEOS = MIXIN_WS_API.packButton(mixin_config.client_id,
                                                 EOS_ASSET_ID, "0.01",
                                                 "EOS pay", "#0080FF")
                buttons = [btnBTC, btnEOS]
                MIXIN_WS_API.sendAppButtonGroup(ws, conversationId, userId,
                                                buttons)
                return
            MIXIN_WS_API.sendUserText(ws, conversationId, userId, realData)
        elif categoryindata == "SYSTEM_ACCOUNT_SNAPSHOT":
            rdJs = json.loads(realData)
            if (float(rdJs["amount"]) > 0):
                mixin_api.transferTo(userId, rdJs["asset_id"], rdJs["amount"],
                                     "")
                    "https://images.mixin.one/HvYGJsV5TGeZ-X9Ek3FEQohQZ3fE9LBEBGcOcn4c4BNHovP4fW4YB97Dg5LcXoQ1hUjMEgjbl1DPlKg1TW7kK6XP=s128",
                    "Pay BTC 0.0001", "topay")
                return
            if 'g' == realData:
                print('send a link APP_BUTTON_GROUP')
                btnBTC = MIXIN_WS_API.packButton(mixin_config.client_id,
                                                 BTC_ASSET_ID, "0.0001",
                                                 "BTC pay")
                btnEOS = MIXIN_WS_API.packButton(mixin_config.client_id,
                                                 EOS_ASSET_ID, "0.01",
                                                 "EOS pay", "#0080FF")
                buttons = [btnBTC, btnEOS]
                MIXIN_WS_API.sendAppButtonGroup(ws, conversationId, userId,
                                                buttons)
                return
            MIXIN_WS_API.sendUserText(ws, conversationId, userId, realData)
        elif categoryindata == "SYSTEM_ACCOUNT_SNAPSHOT":
            rdJs = json.loads(realData)
            if (float(rdJs["amount"]) > 0):
                mixin_api.transferTo(userId, rdJs["asset_id"], rdJs["amount"],
                                     "")


if __name__ == "__main__":

    mixin_api = MIXIN_API(mixin_config)

    mixin_ws = MIXIN_WS_API(on_message=on_message)

    mixin_ws.run()
def on_message(ws, message):
    inbuffer = BytesIO(message)

    f = gzip.GzipFile(mode="rb", fileobj=inbuffer)
    rdata_injson = f.read()
    rdata_obj = json.loads(rdata_injson)
    action = rdata_obj["action"]

    if action not in [
            "ACKNOWLEDGE_MESSAGE_RECEIPT", "CREATE_MESSAGE",
            "LIST_PENDING_MESSAGES"
    ]:
        print("unknow action", action)
        return

    if action == "ACKNOWLEDGE_MESSAGE_RECEIPT":
        return

    if action == "CREATE_MESSAGE":

        data = rdata_obj["data"]
        msgid = data["message_id"]
        typeindata = data["type"]
        categoryindata = data["category"]
        userId = data["user_id"]
        conversationId = data["conversation_id"]
        dataindata = data["data"]
        created_at = data["created_at"]
        updated_at = data["updated_at"]

        realData = base64.b64decode(dataindata)

        MIXIN_WS_API.replayMessage(ws, msgid)

        print('userId', userId)
        print("created_at", created_at)

        if 'error' in rdata_obj:
            return

        if categoryindata not in [
                "SYSTEM_ACCOUNT_SNAPSHOT", "PLAIN_TEXT", "SYSTEM_CONVERSATION",
                "PLAIN_STICKER", "PLAIN_IMAGE", "PLAIN_CONTACT"
        ]:
            print("unknow category", categoryindata)
            return

        if categoryindata == "PLAIN_TEXT" and typeindata == "message":
            realData = realData.decode('utf-8')
            print("dataindata", realData)

            if 'hi' == realData:
                introductionContent = 'welcome to MyFirstRobot\n[hihi] reply n times text\n[c] send a contact card\n[b] send a link button\n[p] you need to pay\n[t] transfer to you'
                MIXIN_WS_API.sendUserText(ws, conversationId, userId,
                                          introductionContent)
                return

            if 'hihi' == realData:
                introductionContent = '你好呀 '
                for i in range(3):
                    MIXIN_WS_API.sendUserText(ws, conversationId, userId,
                                              introductionContent + str(i))
                    time.sleep(1)
                return

            if 'c' == realData:
                print('send a contact card')
                MIXIN_WS_API.sendUserContactCard(
                    ws, conversationId, userId,
                    "d33f7efd-4b0b-41ff-baa3-b22ea40eb44f")
                return

            if 'b' == realData:
                print('send a link button')
                MIXIN_WS_API.sendUserAppButton(
                    ws, conversationId, userId,
                    "https://github.com/includeleec/mixin-python3-sdk",
                    "点我了解 Mixin Python3 SDK")
                return

            if 'p' == realData:
                print('you need to pay')
                CNB_ASSET_ID = "965e5c6e-434c-3fa9-b780-c50f43cd955c"
                MIXIN_WS_API.sendUserPayAppButton(ws, conversationId, userId,
                                                  "给点钱吧", CNB_ASSET_ID, 1,
                                                  "#ff0033")
                return

            if 't' == realData:
                print('transfer to you')
                CNB_ASSET_ID = "965e5c6e-434c-3fa9-b780-c50f43cd955c"
                mixin_api.transferTo(userId, CNB_ASSET_ID, 2, "滴水之恩")
                return

        elif categoryindata == "PLAIN_TEXT":
            print("PLAIN_TEXT but unkonw:")
Beispiel #5
0
def on_message(ws, message):

    inbuffer = BytesIO(message)

    f = gzip.GzipFile(mode="rb", fileobj=inbuffer)
    rdata_injson = f.read()
    rdata_obj = json.loads(rdata_injson)
    action = rdata_obj["action"]

    myPrint( action )
    if action not in ["ACKNOWLEDGE_MESSAGE_RECEIPT", "CREATE_MESSAGE", "LIST_PENDING_MESSAGES"]:
        myPrint("unknow action", action  )
        return

    if action == "ACKNOWLEDGE_MESSAGE_RECEIPT":
        myPrint( "ACKNOWLEDGE_MESSAGE_RECEIPT, return"  )
        return

    if action == "CREATE_MESSAGE":
        data = rdata_obj["data"]
        msgid = data["message_id"]
        typeindata = data["type"]
        categoryindata = data["category"]
        userId = data["user_id"]
        conversationId = data["conversation_id"]
        dataindata = data["data"]
        created_at = data["created_at"]
        updated_at = data["updated_at"]

        realData = base64.b64decode(dataindata)
        MIXIN_WS_API.replayMessage(ws, msgid)

        message_instring = json.dumps( data )
        myPrint( message_instring )

        if 'error' in rdata_obj:
            err = "".format( "error occcur: {}", rdata_obj[ 'error'] )
            myPrint( err )
            return

        if categoryindata not in ["SYSTEM_ACCOUNT_SNAPSHOT", "PLAIN_TEXT", "SYSTEM_CONVERSATION", "PLAIN_STICKER", "PLAIN_IMAGE", "PLAIN_CONTACT"]:
            info = "".format( "unknow category: {}", categoryindata )
            myPrint( info  )
            return

        if categoryindata == "PLAIN_TEXT" and typeindata == "message":
            realData = realData.lower().decode('utf-8')

            if realData in [ "hi", "?", "help" ]:
                introductionContent = 'welcome.\n[hihi] reply n times text\n[c] send a contact card\n[b] send a link button\n[p] you need to pay\n[t] transfer to you'
                MIXIN_WS_API.sendUserText(ws, conversationId, userId, introductionContent)
                return

            if 'hihi' == realData:
                introductionContent = '你好呀 '
                for i in range(3):
                    MIXIN_WS_API.sendUserText(ws, conversationId, userId, introductionContent + str(i))
                    time.sleep(1)
                return

            if 'c' == realData:
                myPrint('send a contact card')
                MIXIN_WS_API.sendUserContactCard(ws, conversationId, userId, get_admin_mixin_id() )
                return

            if 'b' == realData:
                myPrint('send a link button')
                MIXIN_WS_API.sendUserAppButton(ws, conversationId, userId, mixin_api.getRobotHrefLink( 'JinLab' ), mixin_api.getRobotDesc( 'JinLab' ) )
                return

            if 'p' == realData:
                myPrint('you need to pay')
                money = random.randint( 5, 23 ) / 100000
                toPay = "打赏作者: {}".format( money )
                MIXIN_WS_API.sendUserPayAppButton(ws, conversationId, userId, "打赏机器人", mixin_asset_lists.CNB_ASSET_ID,  money, userId )
                return

            if 't' == realData:
                # 机器人 转给 userId
                myPrint('transfer to you')
                myPrint( "userId: %s, jinId: %s" % ( userId, get_admin_mixin_id() ) )
                money = random.randint( 1, 5 ) / 100000
                mxUuid =  "mixin://users/{}".format( robot_config.client_id )
                mixin_api.transferTo( userId, mixin_asset_lists.CNB_ASSET_ID, money, mxUuid )
                txt = "{} 打赏你 {} ".format( mxUuid, money )
                MIXIN_WS_API.sendUserText( ws, conversationId, userId, txt )
                return

        elif categoryindata == "PLAIN_TEXT":
            myPrint("PLAIN_TEXT but unkonw:")
from mixin_ws_api import MIXIN_WS_API
import base64

MASTER_UUID = "0b4f49dc-8fb4-4539-9a89-fb3afc613747"
BTC_ASSET_ID = "c6d0c728-2624-429b-8e0d-d9d19b6592fa"
EOS_ASSET_ID = "6cfe566e-4aad-470b-8c9a-2fd35b49c68d"

btnBTC = MIXIN_WS_API.packButton(MASTER_UUID, BTC_ASSET_ID, "0.0001",
                                 "BTC pay")
btnEOS = MIXIN_WS_API.packButton(MASTER_UUID, EOS_ASSET_ID, "0.01", "EOS pay",
                                 "#0080FF")
print(btnBTC)
print(btnEOS)
buttons = [btnBTC, btnEOS]
print(type(buttons))
buttons_utf = '[' + ','.join(str(btn) for btn in buttons) + ']'
print(buttons_utf)
enButtons = base64.b64encode(
    buttons_utf.encode('utf-8')).decode(encoding='utf-8')
print(enButtons)