Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Telegram bot send message in certain hour in ruby

I would create a telegram bot in ruby that send a message in a determinate hour i decide. For example. It's 8 am and the bot send a message "Good morning". Of course every morning at the same time. It could be a kind of reminder. I can get the time in this way:

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

And i know that to send a message i can use the Telegram API like:

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

I use this code when i have an interaction between the bot and a user. For example:

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
....
....

but I don't know how do what i need because if i want to send a message i need the chat.id and in this case i get it only when i have an interaction with a user. So, is it possibile send a message for example when it's 8 am without any user interaction?

like image 695
Atlas91 Avatar asked Apr 27 '26 19:04

Atlas91


1 Answers

Use crono gem to perform asynchronous tasks.

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

like image 191
jedi Avatar answered Apr 30 '26 08:04

jedi