Telegram 机器人在 ruby 的特定时间发送消息

Telegram bot send message in certain hour in ruby

我会在 ruby 中创建一个电报机器人,它会在我决定的特定时间发送消息。例如。现在是早上 8 点,机器人发送了一条消息 "Good morning"。当然是每天早上同一时间。这可能是一种提醒。我可以这样获取时间:

time = Time.now.strftime("%H:%M")

而且我知道要发送消息,我可以使用 Telegram API,例如:

bot.api.send_message(chat_id: message.chat.id, text: "Hi")

我在机器人和用户之间进行交互时使用此代码。例如:

Telegram::Bot::Client.run(token) do |bot|

  bot.listen do |message|
    case message.text
    when "hi", 'hi@myBot'
      bot.api.send_message(chat_id: message.chat.id, text: "Hi, #{message.from.first_name}")
    end
....
....

但我不知道如何做我需要的,因为如果我想发送消息,我需要 chat.id,在这种情况下,我只有在与用户交互时才会收到它。那么,是否可以在没有任何用户交互的情况下发送消息,例如在早上 8 点?

使用cronogem执行异步任务。

Crono.perform(YourJob).every 1.day, at: {hour: 8, min: 00}