Skip to content

Basic workarounds

Max Melentiev edited this page Jun 28, 2023 · 3 revisions

Here are useful methods to workaround basic telegram issues. They are not included to the gem because they are very application specific. So it's better just to copy them into the project.

Make sure to merge multiple workarounds for same method into single one (ex. edit_message).

UpdatesController

# Ignore stale chats.
def process(*)
  super
rescue Telegram::Bot::Forbidden
  logger.info { 'Reply failed due to stale chat.' }
end

# Telegram requires answer to any callback query, so if you just edit
# the message user will still see spinner in the callback button.
# This module makes #edit_message automatically answers callback queries
# with same text.
def edit_message(type, params = {})
  super
  if type == :text && params[:text] && payload_type == 'callback_query'
    answer_callback_query params[:text]
  end
end

# Telegram responds with error, if you edit message and new content
# is same to current. This patch make it ignore this errors.
def edit_message(type, params = {})
  super
rescue Telegram::Bot::Error => e
  raise unless e.message.include?('message is not modified')
  logger.info { "Ignoring telegram error: #{e.message}" }
end

# Ingore errors that appears when user sends too much callback queries in a short
# time period. Seems like telegram drops first queries before app is able to 
# answer them.
def answer_callback_query(*)
  super
rescue Telegram::Bot::Error => e
  raise unless e.message.include?('QUERY_ID_INVALID')
  logger.info { "Ignoring telegram error: #{e.message}" }
end

Setting a caption for a media group

See #208

def set_media_group_caption(group, caption)
  group.first&.[]=(:caption, caption)
  group
end