💎 ChatSDK Ruby

Conversation History

Fetch previous messages from threads and channels for building context-aware bots.

ChatSDK lets you fetch previous messages from a thread or channel. This is useful for building context-aware bots that need to read back conversation history.

Fetching Messages

Use thread.messages to retrieve messages from a thread:

bot.on_new_mention do |thread, message|
  messages, cursor = thread.messages(limit: 10)

  messages.each do |msg|
    puts "#{msg.author.name}: #{msg.text}"
  end
end

thread.messages returns a two-element array:

  1. An Array of ChatSDK::Message objects
  2. A cursor for pagination (or nil if no more pages)

Pagination

Use the returned cursor to fetch the next page:

all_messages = []
cursor = nil

loop do
  messages, cursor = thread.messages(cursor: cursor, limit: 50)
  all_messages.concat(messages)
  break if cursor.nil?
end

Building Context for AI

A common pattern is collecting conversation history to pass to an AI model:

bot.on_new_mention do |thread, message|
  history, _ = thread.messages(limit: 20)

  context = history.map do |msg|
    role = msg.author.bot? ? "assistant" : "user"
    { role: role, content: msg.text }
  end

  # Pass context to your AI
  response = ai_client.chat(messages: context)
  thread.post(response)
end

Platform Support

PlatformHistory SupportNotes
SlackYesUses conversations.replies for threads, conversations.history for channels
TeamsDeclared but limitedReturns empty results (Bot Framework limitation)
Google ChatYesUses list_messages API
MattermostYesUses get_posts_for_channel
DiscordYesUses get_channel_messages
TelegramNoRaises NotSupportedError
TwilioNoRaises NotSupportedError
MessengerNoRaises NotSupportedError
WhatsAppNoRaises NotSupportedError

Slack, Teams, Google Chat, Mattermost, and Discord declare the :message_history capability. The Teams adapter currently returns empty results because the Bot Framework API does not provide a message history endpoint for bots.

Channel-Level History

For channel-level history (not scoped to a thread), use the adapter's fetch_messages directly:

slack_adapter = bot.adapter(:slack)
messages, cursor = slack_adapter.fetch_messages(channel_id: "C12345", limit: 50)

On this page