Skip to content

narze/telegram-bot-stream-example-ruby-llm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Telegram LLM Bot

A Telegram bot that relays user messages to an LLM and streams responses back using Telegram's sendMessageDraft API for real-time typing animation.

Features

  • Long-polling for incoming messages
  • LLM response streaming via RubyLLM
  • Real-time draft updates with cursor animation () via sendMessageDraft
  • Supports OpenAI, Anthropic, and Gemini providers

Requirements

Getting a Telegram Bot Token

  1. Open Telegram and search for @BotFather
  2. Send /newbot and follow the prompts (choose a name and username)
  3. BotFather will reply with your bot token — copy it into TELEGRAM_BOT_TOKEN in your .env

Setup

cp .env.example .env
# Edit .env with your credentials
bundle install

Configuration

Variable Required Default Description
TELEGRAM_BOT_TOKEN Yes Bot token from @BotFather
OPENAI_API_KEY One required OpenAI API key
ANTHROPIC_API_KEY One required Anthropic API key
GEMINI_API_KEY One required Gemini API key
LLM_MODEL No gpt-4o-mini Model to use (e.g. claude-sonnet-4-5)

Usage

ruby bot.rb

Send any message to your bot and it will stream back an AI-generated response.

How it works

  1. Bot polls for updates via Telegram's getUpdates
  2. On receiving a message, creates a RubyLLM chat session and calls ask with a streaming block
  3. As chunks arrive, accumulated text is sent every ~400ms via sendMessageDraft (same draft_id → Telegram animates the update)
  4. Once streaming completes, the full response is sent via sendMessage, replacing the draft

About

Code example to stream text in Telegram bot using Ruby & RubyLLM

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages