发送消息每隔”x”秒

huangapple go评论92阅读模式
英文:

send message every "x" seconds

问题

I would like to send messages every 5 seconds on different numbers with this code:

  1. const send_message = ["34123456789", "34123456789", "34123456789"];
  2. client.on("ready", () => {
  3. console.log("Client is ready!");
  4. send_message.map((value) => {
  5. const chatId = value + "@c.us";
  6. const message = 'This is a test message';
  7. client.sendMessage(chatId, message);
  8. });
  9. });

Where or how can I add this code to create a correct loop?

  1. await sleep(5000);

我想要使用这段代码每隔5秒发送消息到不同的号码:

  1. const send_message = ["34123456789", "34123456789", "34123456789"];
  2. client.on("ready", () => {
  3. console.log("Client is ready!");
  4. send_message.map((value) => {
  5. const chatId = value + "@c.us";
  6. const message = 'This is a test message';
  7. client.sendMessage(chatId, message);
  8. });
  9. });

请问在哪里或如何添加这段代码以创建正确的循环?

  1. await sleep(5000);
英文:

I would like to send messages every 5 seconds on differents numbers with this code:

  1. const send_message = ["34123456789","34123456789","34123456789"];
  2. client.on("ready", () => {
  3. console.log("Client is ready!");
  4. send_message.map((value) => {
  5. const chatId = value + "@c.us";
  6. const message = 'This is a test message';
  7. client.sendMessage(chatId, message);
  8. });
  9. });

Where or how can I add this code to create a correct bucle?

  1. await sleep(5000);

答案1

得分: 2

请使用 for...of 循环,将客户端的 client.on 回调函数改为 async,如下所示:

  1. const send_message = ["34123456789", "34123456789", "34123456789"];
  2. client.on("ready", async () => {
  3. console.log("Client is ready!");
  4. for (let value of send_message) {
  5. const chatId = value + "@c.us";
  6. const message = 'This is a test message';
  7. client.sendMessage(chatId, message);
  8. await sleep(5000);
  9. }
  10. });

如果你需要进一步的帮助,请告诉我。

英文:

use a for...of loop instead and make the client.on callback async

Something like

  1. const send_message = ["34123456789", "34123456789", "34123456789"];
  2. client.on("ready", async () => {
  3. console.log("Client is ready!");
  4. for (let value of send_message) {
  5. const chatId = value + "@c.us";
  6. const message = 'This is a test message';
  7. client.sendMessage(chatId, message);
  8. await sleep(5000);
  9. }
  10. });

答案2

得分: 0

你只能在异步上下文中使用 await。例如,你可以像这样使用 for .. of 替代 map

  1. client.on("ready", async () => {
  2. for (let value of send_message) {
  3. const chatId = value + "@c.us";
  4. const message = 'This is a test message';
  5. client.sendMessage(chatId, message);
  6. await sleep(5000);
  7. }
  8. })

另外,自 Node 16 起,你不再需要自己创建 sleep(或类似)方法了。你可以使用 timers/promises 模块 中的 setTimeout

英文:

You can use await only in an async context. So you can do like this for instance, using for .. of instead of map

  1. client.on("ready", async () => {
  2. for (let value of send_message) {
  3. const chatId = value + "@c.us";
  4. const message = 'This is a test message';
  5. client.sendMessage(chatId, message);
  6. await sleep(5000);
  7. }
  8. })

BTW since node 16 you don't need to create your own sleep (or similar) method anymore. You can use setTimeout from the timers/promises module

huangapple
  • 本文由 发表于 2023年5月29日 19:47:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76357098.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定