Skip to content

完整示例与调试 ​

天气查询 ​

js
module.exports = {
  async onMessage(ctx) {
    const m = /^天气\s+(.+)$/.exec(ctx.content.trim());
    if (!m) return false;
    const city = m[1].trim();
    try {
      const res = await ctx.http(`https://api.example.com/weather?city=${encodeURIComponent(city)}`);
      if (res.status !== 200) {
        await ctx.reply(`天气接口返回 ${res.status}`);
        return true;
      }
      const data = JSON.parse(res.text);
      await ctx.replyMarkdown(`### ${city}\n\n${data.weather}\n\n温度:${data.temp}℃`);
    } catch (e) {
      ctx.error('天气查询失败', e && e.message);
      await ctx.reply('查询失败,请稍后再试');
    }
    return true;
  },
};

分页菜单 ​

js
module.exports = {
  async onMessage(ctx) {
    const key = `page:${ctx.userId}`;
    if (ctx.content === '下页') {
      const page = Number(ctx.kv.get(key) || '1') + 1;
      ctx.kv.set(key, String(page));
      await ctx.reply(`第 ${page} 页`);
      return true;
    }
    return false;
  },
};

指令按钮 ​

js
const keyboard = {
  content: { rows: [{ buttons: [{
    render_data: { label: '下一页', style: 1 },
    action: { type: 2, permission: { type: 2 }, data: '下页', enter: true },
  }] }] },
};
await ctx.replyMarkdown('### 列表', keyboard);

只看主人 ​

js
if (!ctx.isOwner) {
  await ctx.reply('只有主人可以使用');
  return true;
}

调试 ​

日志:

text
[uplugin:your-slug]

查看:

  • 服务端日志
  • 我的插件 → 最近运行
  • 点击失败记录看完整堆栈

常见问题:

问题排查
不回复是否返回 true、是否调用 reply、是否超时
报错看最近运行堆栈
按钮没反应类型/回调入口是否正确
媒体失败URL 是否公网、大小、回复额度(每插件 2 条 / QQ 总计 5 条)
重启后状态丢是否用了模块变量而不是 KV

薇薇官机平台 · ZL 系列插件