
___x_cmd_weixin_worker(){
    :
}

# first, download all of the data.
# then, check the subscriber list.
# send notification to the subscriber.

___X_CMD_WEIXIN_WORKER_DATA="$___X_CMD_ROOT_DATA/weixin/worker"

___x_cmd_weixin_worker_reactor(){

    printf "%s\n" "$MSG_BODY" >"$___X_CMD_WEIXIN_WORKER_DATA/worker"

    local subscriber

    # interest topic

    while read -r subscriber; do
        printf "%s\n" "$MSG_ID" >"$subscriber"
    done \
        <"$___X_CMD_WEIXIN_WORKER_DATA/subscriber/$MSG_CHATID" \
        >"$___X_CMD_WEIXIN_WORKER_DATA/chat/MSG_LIST.tsv"
}

___x_cmd_weixin_worker_run(){
    ___x_cmd_weixin_react ___x_cmd_weixin_worker_reactor
}



# x weixin recorder start

# x weixin secretary start

___x_cmd_weixin_secretary_run(){

    while true; do
        ___x_cmd_weixin_secretary_run___handler
        case $? in
            130)        return 130
        esac
    done
}

___X_CMD_WEIXIN_SCRETARY_DATA="$___X_CMD_WEIXIN_WORKER_DATA/secretary"

___x_cmd_weixin_secretary_run___handler(){
    {
        ___x_cmd_cmds tail -f "$___X_CMD_WEIXIN_WORKER_DATA/chat/MSG_LIST.tsv"
    } | {
        # process already works

        read -r LAST_MSG_ID<"$___X_CMD_WEIXIN_SCRETARY_DATA/LAST_MSG_ID"
        while read -r line; do
            ___x_cmd pipevar line   ___x_cmd_weixin_secretary_run___handler_tsvline
            printf "%s\n" "$MSG_ID" >"$___X_CMD_WEIXIN_SCRETARY_DATA/LAST_MSG_ID"
        done
    }
}

# x claw start --weixin

___x_cmd_claw_secretary_run___handler_tsvline(){
    local IFS="$___X_CMD_UNSEENCHAR_HT"

    local msg_id
    local msg_chat

    ___x_cmd agent session init --allow-net --sessionid "weixin-chat-master"

    {
        read -r msg_id && read -r msg_chat
        case $? in 130) return 130 ;; esac
    }

    set -- --sessionid "weixin-chat-master"

    case "$MSG_TYPE" in
        image)  set -- "$@" --img "$MSG_IMG_FILEPATH"   ;;
        txt)    set -- "$@" --txt "$MSG_BODY"           ;;
        file)   set -- "$@" --file "$MSG_FILE_PATH"     ;;
    esac

    ___x_cmd agent session handle "$@"

}

