aardio 文档

aardio 范例: 微信 iLink 机器人 - 发送文件消息

//微信 iLink 机器人 - 发送文件消息
import web.rest.iLinkClient.login;

var bot = web.rest.iLinkClient.login()
if(!bot) return;

import console;
console.log("✅ 机器人已上线,正在监听微信消息 ..."); 
console.log("💡 给我发送任意消息,我将回复一个文本文件。");

// 消息监听循环
while(true){
    var updateRes, err, errCode = bot.getUpdates();

    if(updateRes && updateRes.msgs){
        for i, fromMsg in updateRes.msgs {
            var textContent = fromMsg[["item_list"]][[1]][["text_item"]][["text"]];

            if(textContent){
                console.log("📩 [" + fromMsg.from_user_id + "]: " + textContent);

                // 创建一个临时文本文件用于测试
                var tempPath = io.tmpname("weixin-test-", ".txt");
                var fileContent = "这是 aardio iLink Bot 发送的测试文件。\r\n"
                    + "收到消息: " + textContent + "\r\n"
                    + "时间: " + tostring(time()) + "\r\n"
                    + "来自: " + fromMsg.from_user_id;
                string.save(tempPath, fileContent);

                // 发送文件(附带文本说明)
                var ret, sendErr, sendCode = bot.sendFile(fromMsg, tempPath, 
                    "aardio-test.txt", "🤖 收到你的消息,这是测试文件:");

                if(ret){
                    console.log("✅ 文件发送成功: " + tempPath);
                }
                else {
                    console.log("❌ 文件发送失败: " + (sendErr || "") + " (错误码: " + sendCode + ")");
                }
            }
        }
    }
    elseif(!updateRes){
        if(errCode && errCode < 0){
            console.log("⚠️ 业务错误: " + (err || "") + " (" + errCode + "),3秒后重试...");

            if(errCode == -14){
                var bot = web.rest.iLinkClient.login()
                if(!bot) return;
            }
        }
        elseif(errCode){
            console.log("⚠️ 网络错误: " + (err || "") + " (" + errCode + "),3秒后重试...");
        }

        thread.delay(1000);
    }
}

Markdown 格式