2

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?

Atlas91
  • 5,754
  • 17
  • 69
  • 141
  • The only way to send a message without interaction is to hardcode chat id. Do you want to send a message to a certain chat? – yzalavin Jul 20 '18 at 13:08
  • The bot could be in many groups. I can maybe store the chat id in some way and then use it but i don't know if could be the right way. Have you any idea how solve my problem? – Atlas91 Jul 20 '18 at 13:11
  • 2
    You have two options: store ids and send a message by a cron task or create a worker after every message received. Both of them requires user interaction. – yzalavin Jul 20 '18 at 13:13
  • So, shortly, I can't do what I want? – Atlas91 Jul 20 '18 at 13:15

1 Answers1

1

Use crono gem to perform asynchronous tasks.

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

jedi
  • 2,003
  • 5
  • 28
  • 66
  • I have never use crono and it's my first experiment with ruby. Can you please explain how can I use it? Thank you very much – Atlas91 Jul 20 '18 at 13:25
  • Just go the the gem git repository I linked above. The usage of the gem is described in details there. – jedi Jul 20 '18 at 14:17
  • Did I help you with your problem? Can you accept my answer? @End.Game – jedi Jul 25 '18 at 19:14