Class: ChatSDK::Telegram::Adapter

Inherits:
Adapter::Base
  • Object
show all
Defined in:
lib/chat_sdk/telegram/adapter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bot_token: nil, secret_token: nil, bot_username: nil) ⇒ Adapter

Returns a new instance of Adapter.

Raises:

  • (ChatSDK::ConfigurationError)


13
14
15
16
17
18
19
20
21
22
# File 'lib/chat_sdk/telegram/adapter.rb', line 13

def initialize(bot_token: nil, secret_token: nil, bot_username: nil)
  @bot_token = bot_token || ENV["TELEGRAM_BOT_TOKEN"]
  @secret_token = secret_token || ENV["TELEGRAM_WEBHOOK_SECRET_TOKEN"]
  @bot_username = bot_username || ENV["TELEGRAM_BOT_USERNAME"]

  raise ChatSDK::ConfigurationError, "Telegram bot_token required" unless @bot_token

  @client = ApiClient.new(@bot_token)
  @renderer = KeyboardRenderer.new
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



11
12
13
# File 'lib/chat_sdk/telegram/adapter.rb', line 11

def client
  @client
end

Instance Method Details

#ack_response(_rack_request) ⇒ Object



45
46
47
# File 'lib/chat_sdk/telegram/adapter.rb', line 45

def ack_response(_rack_request)
  nil
end

#add_reaction(channel_id:, message_id:, emoji:) ⇒ Object



95
96
97
98
99
100
101
# File 'lib/chat_sdk/telegram/adapter.rb', line 95

def add_reaction(channel_id:, message_id:, emoji:)
  @client.set_message_reaction(
    chat_id: channel_id,
    message_id: message_id,
    reaction: [{"type" => "emoji", "emoji" => emoji}]
  )
end

#delete_message(channel_id:, message_id:) ⇒ Object



81
82
83
# File 'lib/chat_sdk/telegram/adapter.rb', line 81

def delete_message(channel_id:, message_id:)
  @client.delete_message(chat_id: channel_id, message_id: message_id)
end

#edit_message(channel_id:, message_id:, message:) ⇒ Object



70
71
72
73
74
75
76
77
78
79
# File 'lib/chat_sdk/telegram/adapter.rb', line 70

def edit_message(channel_id:, message_id:, message:)
  text, reply_markup = prepare_message_payload(message)

  @client.edit_message_text(
    chat_id: channel_id,
    message_id: message_id,
    text: text,
    reply_markup: reply_markup
  )
end

#mention(user_id) ⇒ Object



119
120
121
# File 'lib/chat_sdk/telegram/adapter.rb', line 119

def mention(user_id)
  "[user](tg://user?id=#{user_id})"
end

#nameObject



24
25
26
# File 'lib/chat_sdk/telegram/adapter.rb', line 24

def name
  :telegram
end

#open_dm(user_id) ⇒ Object



111
112
113
# File 'lib/chat_sdk/telegram/adapter.rb', line 111

def open_dm(user_id)
  user_id
end

#parse_events(rack_request) ⇒ Object



49
50
51
52
53
54
# File 'lib/chat_sdk/telegram/adapter.rb', line 49

def parse_events(rack_request)
  payload = read_json_body(rack_request)
  EventParser.parse(payload, bot_username: @bot_username)
rescue JSON::ParserError
  []
end

#post_message(channel_id:, message:, thread_id: nil) ⇒ Object

Outbound



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/chat_sdk/telegram/adapter.rb', line 57

def post_message(channel_id:, message:, thread_id: nil)
  text, reply_markup = prepare_message_payload(message)

  result = @client.send_message(
    chat_id: channel_id,
    text: text,
    reply_markup: reply_markup,
    reply_to_message_id: thread_id
  )

  parse_telegram_message(result, channel_id)
end

#remove_reaction(channel_id:, message_id:, emoji:) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



103
104
105
106
107
108
109
# File 'lib/chat_sdk/telegram/adapter.rb', line 103

def remove_reaction(channel_id:, message_id:, emoji:) # rubocop:disable Lint/UnusedMethodArgument
  @client.set_message_reaction(
    chat_id: channel_id,
    message_id: message_id,
    reaction: []
  )
end

#render(postable_message) ⇒ Object



123
124
125
126
127
128
129
# File 'lib/chat_sdk/telegram/adapter.rb', line 123

def render(postable_message)
  if postable_message.card?
    @renderer.render(postable_message.card)
  else
    postable_message.text
  end
end

#start_typing(channel_id:, thread_id: nil) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



115
116
117
# File 'lib/chat_sdk/telegram/adapter.rb', line 115

def start_typing(channel_id:, thread_id: nil) # rubocop:disable Lint/UnusedMethodArgument
  @client.send_chat_action(chat_id: channel_id, action: "typing")
end

#upload_file(channel_id:, io:, filename:, thread_id: nil, comment: nil) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'lib/chat_sdk/telegram/adapter.rb', line 85

def upload_file(channel_id:, io:, filename:, thread_id: nil, comment: nil)
  @client.send_document(
    chat_id: channel_id,
    document: io,
    filename: filename,
    caption: comment,
    reply_to_message_id: thread_id
  )
end

#verify_request!(rack_request) ⇒ Object

Inbound



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/chat_sdk/telegram/adapter.rb', line 29

def verify_request!(rack_request)
  return true unless @secret_token

  header_token = rack_request.get_header("HTTP_X_TELEGRAM_BOT_API_SECRET_TOKEN")

  unless header_token
    raise ChatSDK::SignatureVerificationError, "Missing Telegram secret token header"
  end

  unless Rack::Utils.secure_compare(header_token, @secret_token)
    raise ChatSDK::SignatureVerificationError, "Invalid Telegram secret token"
  end

  true
end