简体   繁体   English

如何使用 npm 库 Telegraf 从数据库中的 id 列表一次向所有电报机器人用户发送消息

[英]How to send messages to all telegram bot users from a list of ids in a database at once using npm library Telegraf

i have selected all ids of my telegram bot users from mysql database and want to send messages to them at once.I have tried我已经从 mysql 数据库中选择了我的电报机器人用户的所有 ID,并希望立即向他们发送消息。我试过了

con.query("SELECT id FROM account",function (err,res) {
           res.forEach(function (message) {
               ctx.telegram.sendMessage(message.id, ctx.message.text)
   })
})

but it sends each message to a user at a time.i want to send the message to all users at once using an array of all ids as in telegram api但它一次将每条消息发送给一个用户。我想使用电报 api 中的所有 id 数组一次将消息发送给所有用户

Use sleep in your loop to send for each user:在循环中使用 sleep 为每个用户发送:

function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}
async function send() {
   con.query("SELECT id FROM account",function (err,res) {
       res.forEach(function (message) {
           ctx.telegram.sendMessage(message.id, ctx.message.text)
           await sleep(2000);
      })
   })
 }

Good luck;祝你好运;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Telegram bot(telegraf.js 库)将消息从一个通道转发到另一个通道方法? - Telegram bot ( telegraf.js library ) forwarding messages from one to another channel method? 如何使用 Telegraf.js Node.js 在 Telegram Bot 中发送私人消息 - How to send private message in Telegram Bot using Telegraf.js Node.js 删除@ids 和文本包含来自电报机器人消息的链接 - delete @ids and text contain link from messages of telegram bot 带有 telegraf.js 的 Telegram 机器人:无法使用 flickr api 发送随机照片进行聊天 - Telegram bot with telegraf.js : can't send random photo to chat using flickr api Telegraf 从 Telegram API 发送图像 - Telegraf send an image from Telegram API 如何在没有用户输入的情况下发送消息Telegram BOT API - How to send messages without user inputs Telegram BOT API Telegraf 和 Cloud Functions:如何阻止 Telegram Bot 接收无休止的旧更新 - Telegraf and Cloud Functions: How to stop Telegram Bot from receiving endless old update 如何用文字编辑内联按钮? 电报机器人,Telegraf,节点 - How to edit inline buttons with text ? telegram bot, telegraf, node 如何从电报机器人将数据插入数据库? - How to insert data into a database from a telegram bot? Telegram Bot:从私人群组转发消息 - Telegram Bot: Forwarding Messages from Private Group
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM