我会用 ruby 创建一个电报机器人,它会在我决定的特定时间发送消息。例如。现在是早上 8 点,机器人发送了一条消息“早上好”。当然是每天早上同一时间。这可能是一种提醒。我可以通过这种方式获取时间:
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 点?
最佳答案
使用crono gem 来执行异步任务。
Crono.perform(YourJob). 每 1 天,时间:{小时:8,分钟:00}
关于ruby - 电报机器人在特定时间用 ruby 发送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51443056/