aardio 文档

aardio 范例: 微信 iLink 机器人 - 发送图片消息

自定义登录界面

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

/*
在工作线程里可以用 web.rest.iLinkClient.login 登录比较方便。
自定义登录界面: https://www.aardio.com/zh-cn/doc/example/Web/REST/iLink/weixin-bot.html
*/
var bot = web.rest.iLinkClient.login()
if(!bot) return;

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

// 用于测试的图片(程序根目录下的图片)
var testImagePath = "/test-image.jpg";

// 如果没有测试图片,可以创建一个简单的测试图片
if(!io.exist(testImagePath)){
    // 使用 gdip 创建一个简单的测试图片
    import gdip.bitmap;
    var bmp = gdip.bitmap(400, 300);
    var graphics = bmp.getGraphics();
    graphics.clear(0xFFFFFFFF);

    var font = gdip.font("Tahoma", 24, 0/*_FontStyleRegular*/, 3/*_UnitPoint*/);
    var brush = gdip.solidBrush(0xFF0078D4);
    var format = gdip.stringformat();
    format.align = 1/*_StringAlignmentCenter*/;
    format.lineAlign = 1/*_StringAlignmentCenter*/;

    graphics.drawString("Hello from aardio!\n微信 iLink Bot", font, 
        ::RECTF(0, 0, 400, 300), format, brush);

    bmp.save(testImagePath);
    font.delete();
    brush.delete();
    format.delete();
    graphics.delete();
    bmp.delete();

    console.log("📷 已创建测试图片: " + io.fullpath(testImagePath));
}

// 消息监听循环
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 ret, sendErr, sendCode = bot.sendImage(fromMsg, testImagePath, 
                    "🤖 收到你的消息: " + textContent + "\n这是 aardio 生成的测试图片");

                if(ret){
                    console.log("✅ 图片发送成功");
                }
                else {
                    console.log("❌ 图片发送失败: " + (sendErr || "")  );
                }
            }
        }
    }
    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 格式