crgram

Telegram Bot API library for Crystal

crgram

Telegram Bot API library for Crystal

Started based on Bot API v9.2

  • Note: Under Development.

Installation

  1. Add the dependency to your shard.yml:

    dependencies:
      crgram:
        github: zerohxyz/crgram
    
  2. Run shards install

Usage

require "crgram"

BOT_TOKEN = "BOT_TOKEN"

bot = Crgram::Bot.new(BOT_TOKEN)

bot.on_text_msg do |msg, ctx, text|
  if from = msg.from
    ctx.reply(from.id, text)
  end
end

bot.start

or

require "crgram"

BOT_TOKEN = "BOT_TOKEN"

module MyBot
  include Crgram

  bot = Bot.new(BOT_TOKEN)
  
  bot.on_text_msg do |msg, ctx, text|
    if from = msg.from
      ctx.reply(from.id, text)
    end
  end
  
  bot.start
end

For webhook implementation create bot with webhook options

  bot = Crgram::Bot.new(BOT_TOKEN, Crgram::WebhookOptions.new("URL"))

Handlers

Event Handlers

bot.on_{{event_name}} do |event, ctx|
  # 'event' type is tied to that specific handler
  # for message event it  will be of type Crgram::Message
  
  # 'ctx' is Crgram::Core::Context
end
Handler Method Yielded Event (Type)
on_message Message
on_edited_message Message
on_channel_post Message
on_edited_channel_post Message
on_business_connection BusinessConnection
on_business_message Message
on_edited_business_message Message
on_deleted_business_messages BusinessMessagesDeleted
on_message_reaction MessageReactionUpdated
on_message_reaction_count MessageReactionCountUpdated
on_inline_query InlineQuery
on_chosen_inline_result ChosenInlineResult
on_callback_query CallbackQuery
on_shipping_query ShippingQuery
on_pre_checkout_query PreCheckoutQuery
on_purchased_paid_media PaidMediaPurchased
on_poll Poll
on_poll_answer PollAnswer
on_my_chat_member ChatMemberUpdated
on_chat_member ChatMemberUpdated
on_chat_join_request ChatJoinRequest
on_chat_boost ChatBoostUpdated
on_removed_chat_boost ChatBoostRemoved

Message Handlers

bot.on_{{message_subtype_name}}_msg do |msg, ctx, message_subtype|
  # msg of type Crgram::Message
  # ctx of type Crgram::Core::Context
  # message_subtype for sticker is Crgram::Sticker
end
  • on_text_msg
  • on_sticker_msg
  • on_audio_msg
  • on_video_msg
  • on_document_msg
  • on_animation_msg
  • on_voice_msg
  • on_video_note_msg
  • on_poll_msg

Command Handler

  • on_command_msg
  bot.on_command_msg do |msg, ctx, cmd, args|
      # cmd is string ,eg: start
      # args is array of string
  end

Contributing

  1. Fork it (https://github.com/zerohxyz/crgram/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

  • zeroh - creator and maintainer
Repository

crgram

Owner
Statistic
  • 0
  • 0
  • 0
  • 0
  • 0
  • about 4 hours ago
  • June 29, 2026
License

MIT License

Links
Synced at

Mon, 29 Jun 2026 02:56:20 GMT

Languages