You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Telegram机器人发送用户列表时出现消息文本为空错误

Telegram Bot发送用户列表时报错“message text is empty”

需求与问题

要展示指定邀请者的所有用户列表,通过User.find({Inviter: helpers.getUserID(query)})查询数据后,用map生成包含用户ID的条目数组,但调用bot.sendMessage时触发400错误,提示message text is empty。普通HTML字符串能正常发送,尝试其他方法未解决问题。

相关代码

User.find({Inviter: helpers.getUserID(query)})
    .then(inviters => {
    
        console.log(inviters)
    
        let html = inviters.map((f, i) => {
            return `${i+1}. ${f._id} `
        })
        console.log(html)
        
        bot.sendMessage(chatID, html, {
            parse_mode: "HTML"
        })
    
    })

错误栈信息

Unhandled rejection Error: ETELEGRAM: 400 Bad Request: message text is empty
    at C:\Users\Basanas\Desktop\DeclBOT\node_modules\node-telegram-bot-api\src\telegram.js:299:15
    at tryCatcher (C:\Users\Basanas\Desktop\DeclBOT\node_modules\bluebird\js\release\util.js:16:23)
    at Promise._settlePromiseFromHandler (C:\Users\Basanas\Desktop\DeclBOT\node_modules\bluebird\js\release\promise.js:547:31)
    at Promise._settlePromise (C:\Users\Basanas\Desktop\DeclBOT\node_modules\bluebird\js\release\promise.js:604:18)
    at Promise._settlePromise0 (C:\Users\Basanas\Desktop\DeclBOT\node_modules\bluebird\js\release\promise.js:649:10)
    at Promise._settlePromises (C:\Users\Basanas\Desktop\DeclBOT\node_modules\bluebird\js\release\promise.js:729:18)
    at _drainQueueStep (C:\Users\Basanas\Desktop\DeclBOT\node_modules\bluebird\js\release\async.js:93:12)
    at _drainQueue (C:\Users\Basanas\Desktop\DeclBOT\node_modules\bluebird\js\release\async.js:86:9)
    at Async._drainQueues (C:\Users\Basanas\Desktop\DeclBOT\node_modules\bluebird\js\release\async.js:102:5)
    at Async.drainQueues [as _onImmediate] (C:\Users\Basanas\Desktop\DeclBOT\node_modules\bluebird\js\release\async.js:15:14)
    at process.processImmediate (node:internal/timers:478:21)

原因分析与解决办法

原因

bot.sendMessage的第二个参数要求是字符串类型,但你直接传入了map返回的数组:

  • 如果查询结果为空数组,map返回空数组,转成字符串后就是空文本,触发Telegram的报错;
  • 即使有查询结果,直接传数组也不符合API参数要求。

解决办法

  1. 将数组拼接为字符串:用join('\n')把数组元素按换行拼接成字符串,每个用户条目单独一行:
    let html = inviters.map((f, i) => {
        return `${i+1}. ${f._id}`
    }).join('\n'); // 拼接成字符串
    
  2. 处理空结果场景:避免发送空文本,给空结果设置默认提示:
    // 检查拼接后的文本是否为空
    if (!html) {
        html = "暂无邀请的用户";
    }
    bot.sendMessage(chatID, html, { parse_mode: "HTML" });
    

内容的提问来源于stack exchange,提问作者Basanas Lokwen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 07:05:57