# aardio 范例: 微信 iLink 机器人 - 综合媒体发送（自动检测文件类型）

```aardio
//微信 iLink 机器人 - 综合媒体发送（自动检测文件类型）
import web.rest.iLinkClient.login;

var bot = web.rest.iLinkClient.login()
if(!bot) return;

import console;
console.log("✅ 机器人已上线，正在监听微信消息 ..."); 

/*
支持的媒体类型（自动根据扩展名检测）：
- 图片: .jpg .jpeg .png .gif .bmp .webp .svg .ico
- 视频: .mp4 .avi .mov .mkv .wmv .flv .webm .3gp
- 文件: 其他所有扩展名

使用 sendMediaByPath 自动检测类型，也可以用更精确的：
- bot.sendImage(fromMsg, filePath, text)
- bot.sendVideo(fromMsg, filePath, text)  
- bot.sendFile(fromMsg, filePath, fileName, text)
*/

console.log("💡 发送以下命令测试：");
console.log("   '图片' → 发送测试图片");
console.log("   '文件' → 发送测试文本文件");
console.log("   '路径 C:\\test.png' → 发送指定文件");

// 创建一个测试图片
var testImagePath = io.fullpath("/ilink-test-image.jpg");
if(!io.exist(testImagePath)){
	import gdip.bitmap;
	var bmp = gdip.bitmap(400, 300);
	var graphics = bmp.getGraphics();
	graphics.clear(0xFFFFFFFF);
	
	var font = gdip.font("Tahoma", 20, 0, 3/*_UnitPoint*/);
	var brush = gdip.solidBrush(0xFF0078D4);
	var format = gdip.stringformat();
	format.align = 1/*_StringAlignmentCenter*/;
	format.lineAlign = 1/*_StringAlignmentCenter*/;
	
	graphics.drawString("aardio iLink Bot\n\n🖼️ 测试图片", font, 
		::RECTF(0, 0, 400, 300), format, brush);
	
	bmp.save(testImagePath);
	font.delete();
	brush.delete();
	format.delete();
	graphics.delete();
	bmp.delete();
}

// 消息监听循环
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) continue;
			console.log("📩 [" + fromMsg.from_user_id + "]: " + textContent);
			
			var filePath;
			var replyText;
			var cmd = string.lower(string.trim(textContent));
			
			if(cmd == "图片"){
				filePath = testImagePath;
				replyText = "🤖 这是 aardio 生成的测试图片";
			}
			elseif(cmd == "文件"){
				// 创建临时文件
				var tmpPath = io.tmpname("weixin-", ".txt");
				string.save(tmpPath, "aardio iLink Bot 测试文件\n时间: " + tostring(time()));
				filePath = tmpPath;
				replyText = "🤖 这是 aardio 生成的测试文件";
			}
			else {
				// 匹配 "路径 xxx" 命令
				var userPath = string.match(textContent, "^路径\s+(.+)$");
				if(userPath && io.exist(userPath)){
					filePath = userPath;
					replyText = "🤖 发送文件: " + io.splitpath(filePath).file;
				}
				else {
					// 默认回复文本
					bot.sendMessage(fromMsg, "🤖 发送 '图片' 或 '文件' 测试媒体发送功能");
					continue;
				}
			}
			
			if(filePath){
				// 自动检测类型并发送
				var ret, sendErr, sendCode = bot.sendMediaByPath(fromMsg, filePath, replyText);
				if(ret){
					console.log("✅ 媒体发送成功: " + filePath);
				}
				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);
	}
}

```