Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

API methods examples

Luis Mayoral edited this page Oct 21, 2015 · 3 revisions

Always start creating a new instance of Telegrammer::Bot with your bot's API Token:

bot = Telegrammer::Bot.new('[YOUR TELEGRAM TOKEN]')

Getting updates

# https://core.telegram.org/bots/api/#getupdates
bot.get_updates do |message|
  puts "In chat #{message.chat.id}, @#{message.from.username} said: #{message.text}"
  bot.send_message(chat_id: message.chat.id, text: "You said: #{message.text}")

  # Here you can also process message text to detect user commands
  # To learn more about commands, see https://core.telegram.org/bots#commands
end

Sending messages

# https://core.telegram.org/bots/api/#sendmessage
bot.send_message(chat_id: 123456789, text: "This is a text")

# https://core.telegram.org/bots/api/#replykeyboardmarkup
reply_markup = Telegrammer::DataTypes::ReplyKeyboardMarkup.new(
  keyboard: [
    ["Option 1.1", "Option 1.2"],
    ["Option 2"],
    ["Option 3.1", "Option 3.2", "Option 3.3"]
  ],
  resize_keyboard: false
)

# This message will activate a custom keyboard...
bot.send_message(
  chat_id: 123456789,
  text: "Select an option",
  reply_markup: reply_markup
)

# https://core.telegram.org/bots/api/#replykeyboardhide
reply_markup = Telegrammer::DataTypes::ReplyKeyboardHide.new(
  hide_keyboard: true
)

# And this message will disable it
bot.send_message(
  chat_id: 123456789,
  text: "Thanks",
  reply_markup: reply_markup
)

Forwarding messages

# https://core.telegram.org/bots/api/#forwardmessage
bot.forward_message(chat_id: 123456789, from_chat_id: 987654321, message_id: 111222333)

Sending photos

# https://core.telegram.org/bots/api/#sendphoto
image_file = File.open("foo.jpg")
bot.send_photo(chat_id: 123456789, photo: image_file)

Sending audio files

# https://core.telegram.org/bots/api/#sendaudio
audio_file = File.open("foo.ogg")
bot.send_audio(chat_id: 123456789, audio: audio_file)

Sending documents

# https://core.telegram.org/bots/api/#senddocument
my_secret_file = File.open("secret.txt")
bot.send_document(chat_id: 123456789, document: my_secret_file)

Sending stickers

# https://core.telegram.org/bots/api/#sendsticker
sticker_image_file = File.open("sticker.jpg")
bot.send_sticker(chat_id: 123456789, sticker: sticker_image_file)

Sending videos

# https://core.telegram.org/bots/api/#sendvideo
video_file = File.open("foo.mp4")
bot.send_video(chat_id: 123456789, video: video_file)

Sending locations

# https://core.telegram.org/bots/api/#sendlocation
bot.send_location(chat_id: 123456789, latitude: 38.775539, longitude: -4.829988)

Sending chat actions

# https://core.telegram.org/bots/api/#sendchataction
bot.send_chat_action(chat_id: 123456789, action: "typing")

Getting user profile photos

# https://core.telegram.org/bots/api/#getuserprofilephotos
bot.get_user_profile_photos(user_id: 123456789)

Getting files

# https://core.telegram.org/bots/api/#getfile
bot.get_file(file_id: 123456789)