Class: ChatSDK::Telegram::ApiClient

Inherits:
Object
  • Object
show all
Defined in:
lib/chat_sdk/telegram/api_client.rb

Constant Summary collapse

BASE_URL =
"https://api.telegram.org"

Instance Method Summary collapse

Constructor Details

#initialize(bot_token) ⇒ ApiClient

Returns a new instance of ApiClient.



8
9
10
# File 'lib/chat_sdk/telegram/api_client.rb', line 8

def initialize(bot_token)
  @bot_token = bot_token
end

Instance Method Details

#delete_message(chat_id:, message_id:) ⇒ Object



25
26
27
# File 'lib/chat_sdk/telegram/api_client.rb', line 25

def delete_message(chat_id:, message_id:)
  request(:post, "deleteMessage", {"chat_id" => chat_id, "message_id" => message_id})
end

#edit_message_text(chat_id:, message_id:, text:, reply_markup: nil) ⇒ Object



19
20
21
22
23
# File 'lib/chat_sdk/telegram/api_client.rb', line 19

def edit_message_text(chat_id:, message_id:, text:, reply_markup: nil)
  body = {"chat_id" => chat_id, "message_id" => message_id, "text" => text, "parse_mode" => "Markdown"}
  body["reply_markup"] = reply_markup if reply_markup
  request(:post, "editMessageText", body)
end

#send_chat_action(chat_id:, action:) ⇒ Object



49
50
51
# File 'lib/chat_sdk/telegram/api_client.rb', line 49

def send_chat_action(chat_id:, action:)
  request(:post, "sendChatAction", {"chat_id" => chat_id, "action" => action})
end

#send_document(chat_id:, document:, filename:, caption: nil, reply_to_message_id: nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/chat_sdk/telegram/api_client.rb', line 29

def send_document(chat_id:, document:, filename:, caption: nil, reply_to_message_id: nil)
  payload = {
    "chat_id" => chat_id,
    "document" => Faraday::Multipart::FilePart.new(document, "application/octet-stream", filename)
  }
  payload["caption"] = caption if caption
  payload["reply_to_message_id"] = reply_to_message_id if reply_to_message_id

  response = upload_connection.post(api_path("sendDocument"), payload)
  handle_response(response)
end

#send_message(chat_id:, text:, reply_markup: nil, reply_to_message_id: nil) ⇒ Object



12
13
14
15
16
17
# File 'lib/chat_sdk/telegram/api_client.rb', line 12

def send_message(chat_id:, text:, reply_markup: nil, reply_to_message_id: nil)
  body = {"chat_id" => chat_id, "text" => text, "parse_mode" => "Markdown"}
  body["reply_markup"] = reply_markup if reply_markup
  body["reply_to_message_id"] = reply_to_message_id if reply_to_message_id
  request(:post, "sendMessage", body)
end

#set_message_reaction(chat_id:, message_id:, reaction:) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/chat_sdk/telegram/api_client.rb', line 41

def set_message_reaction(chat_id:, message_id:, reaction:)
  request(:post, "setMessageReaction", {
    "chat_id" => chat_id,
    "message_id" => message_id,
    "reaction" => reaction
  })
end