atipugin / telegram-bot-ruby

Ruby wrapper for Telegram's Bot API

Home Page:https://core.telegram.org/bots/api

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How do I stop my bot without repeating the last message, next time I start it up?

protektwar opened this issue · comments

How can I stop this bot and flush/acknowledge the last message received, without having it repeated next time when I start the bot?
For instance it receive a "/quit" command and it run an exit(true) in my code. Next time when I start my bot it will stop right away as the last message/command is repeating...

Hi @protektwar, sounds interesting. I didn't mention that messages get duplicated after stop/start or restart though I didn't do it intentionally.
I think it is all about offset option in Client. It is getting updated before yielding message to a block specified.
I guess you can try to pass initial offset on Client initialization. You could store offset somehow while handling /quit command before exiting application.

  def saveLastMsgID(id)
    File.open(@lastMsgID_filename, "wb") { |file|
      file.write(id)
    }
  end

  def loadLastMsgID
    File.open(@lastMsgID_filename){ |file|
    @lastMsgID = file.gets
    }
  end

and inside bot.listen block

 bot.listen do |message|
        if ( bot.options[:offset].to_s == @lastMsgID.to_s )
         break
       end
 end

It is not what I meant but it is up to you. If the problem is solved please close the issue.