Class: ChatSDK::Telegram::Adapter
- Inherits:
-
Adapter::Base
- Object
- Adapter::Base
- ChatSDK::Telegram::Adapter
- Defined in:
- lib/chat_sdk/telegram/adapter.rb
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
Instance Method Summary collapse
- #ack_response(_rack_request) ⇒ Object
- #add_reaction(channel_id:, message_id:, emoji:) ⇒ Object
- #delete_message(channel_id:, message_id:) ⇒ Object
- #edit_message(channel_id:, message_id:, message:) ⇒ Object
-
#initialize(bot_token: nil, secret_token: nil, bot_username: nil) ⇒ Adapter
constructor
A new instance of Adapter.
- #mention(user_id) ⇒ Object
- #name ⇒ Object
- #open_dm(user_id) ⇒ Object
- #parse_events(rack_request) ⇒ Object
-
#post_message(channel_id:, message:, thread_id: nil) ⇒ Object
Outbound.
-
#remove_reaction(channel_id:, message_id:, emoji:) ⇒ Object
rubocop:disable Lint/UnusedMethodArgument.
- #render(postable_message) ⇒ Object
-
#start_typing(channel_id:, thread_id: nil) ⇒ Object
rubocop:disable Lint/UnusedMethodArgument.
- #upload_file(channel_id:, io:, filename:, thread_id: nil, comment: nil) ⇒ Object
-
#verify_request!(rack_request) ⇒ Object
Inbound.
Constructor Details
#initialize(bot_token: nil, secret_token: nil, bot_username: nil) ⇒ Adapter
Returns a new instance of Adapter.
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
#client ⇒ Object (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.( chat_id: channel_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 (channel_id:, message_id:) @client.(chat_id: channel_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 (channel_id:, message_id:, message:) text, reply_markup = () @client.( chat_id: channel_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 |
#name ⇒ Object
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 (channel_id:, message:, thread_id: nil) text, reply_markup = () result = @client.( chat_id: channel_id, text: text, reply_markup: reply_markup, reply_to_message_id: thread_id ) (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.( chat_id: channel_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() if .card? @renderer.render(.card) else .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 |