atipugin / telegram-bot-ruby

Ruby wrapper for Telegram's Bot API
https://core.telegram.org/bots/api
Do What The F*ck You Want To Public License
1.35k stars 217 forks source link

Question. How do i listen only new message after bot was started? #270

Closed fedorc closed 1 year ago

fedorc commented 1 year ago

Problem: Once i start a bot, it will go back in a chat history and iterate through all messages which was sent while bot was down/disabled.

What is a way to ignore messages with timestamp which is less than time.now (which is a time when bot was started)

Thank you in advance.

atipugin commented 1 year ago

Hey @fedorc,

unfortunately, I don't think it's possible, you have to implement it manually somehow. The problem is that Update object itself doesn't provide any date information, only Update's ID. You can try to check Update#current_message for dates, but this is not 100% reliable because some types (e.g. Message) provide such information, but others (e.g. ChosenInlineResult) don't.

fedorc commented 1 year ago

@atipugin , thank you for your reply, it pointed me in a different direction and i was able to solve my need

i used to have, which was pooling old messages

bot.listen do |message|
  if message.text.match(/^(\d)+$/) && message.text.size == 5
    # do some good stuff
  end
end

changed to this, and now its working as i need

time_now = Time.now.to_i
bot.listen do |message|
  if (message.date.to_i > time_now) && message.text.match(/^(\d)+$/) && message.text.size == 5
    # do some good stuff
  end
end

can be closed. thank you