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



98
99
100
101
102
103
104
# File 'lib/chat_sdk/telegram/adapter.rb', line 98

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



84
85
86
# File 'lib/chat_sdk/telegram/adapter.rb', line 84

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



73
74
75
76
77
78
79
80
81
82
# File 'lib/chat_sdk/telegram/adapter.rb', line 73

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



122
123
124
# File 'lib/chat_sdk/telegram/adapter.rb', line 122

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



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

def open_dm(user_id)
  user_id
end

#parse_events(rack_request) ⇒ Object



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

def parse_events(rack_request)
  body = rack_request.body.read
  rack_request.body.rewind

  payload = JSON.parse(body)
  EventParser.parse(payload, bot_username: @bot_username)
rescue JSON::ParserError
  []
end

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

Outbound



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/chat_sdk/telegram/adapter.rb', line 60

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



106
107
108
109
110
111
112
# File 'lib/chat_sdk/telegram/adapter.rb', line 106

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



126
127
128
129
130
131
132
# File 'lib/chat_sdk/telegram/adapter.rb', line 126

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



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

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



88
89
90
91
92
93
94
95
96
# File 'lib/chat_sdk/telegram/adapter.rb', line 88

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