Class: FlowChat::Telegram::Client
- Inherits:
-
Object
- Object
- FlowChat::Telegram::Client
- Includes:
- Instrumentation
- Defined in:
- lib/flow_chat/telegram/client.rb
Constant Summary
Constants included from Instrumentation
Instrumentation::DELIVERED_MESSAGE_ID_KEY, Instrumentation::DELIVERY_DURATION_KEY
Instance Method Summary collapse
- #answer_callback_query(callback_query_id, text: nil, show_alert: false) ⇒ Object
- #delete_message(chat_id, message_id) ⇒ Object
- #delete_webhook ⇒ Object
-
#download_file(file_id) ⇒ Object
Download the raw bytes for an inbound file_id.
- #edit_message_text(chat_id, message_id, text, keyboard: nil, parse_mode: "HTML") ⇒ Object
-
#file_url(file_id) ⇒ Object
Build the download URL for an inbound file_id.
-
#get_file(file_id) ⇒ Object
Get file metadata (including file_path) for an inbound file_id.
- #get_me ⇒ Object
- #get_webhook_info ⇒ Object
-
#indicate_typing(chat_id) ⇒ Hash
Show a typing indicator in a Telegram chat.
-
#initialize(config) ⇒ Client
constructor
A new instance of Client.
- #send_audio(chat_id, audio_url_or_id, caption: nil) ⇒ Object
-
#send_chat_action(chat_id, action: "typing") ⇒ Hash
Send a chat action (e.g. typing indicator) to a Telegram chat.
- #send_document(chat_id, document_url_or_id, caption: nil) ⇒ Object
-
#send_message(chat_id, prompt, choices: nil, media: nil) ⇒ Object
Main send_message method matching FlowChat pattern.
- #send_photo(chat_id, photo_url_or_id, caption: nil) ⇒ Object
- #send_photo_with_keyboard(chat_id, photo_url_or_id, caption: nil, keyboard: nil) ⇒ Object
- #send_text(chat_id, text, parse_mode: "HTML") ⇒ Object
- #send_text_with_keyboard(chat_id, text, keyboard, parse_mode: "HTML") ⇒ Object
- #send_video(chat_id, video_url_or_id, caption: nil) ⇒ Object
- #send_voice(chat_id, voice_url_or_id) ⇒ Object
-
#set_webhook(url, secret_token: nil, allowed_updates: nil) ⇒ Object
Webhook management.
Methods included from Instrumentation
#elapsed_ms_since, #inbound_message?, #instrument, instrument, #platform_message_id_from, report_api_error, #report_delivery_failure, #report_delivery_success, #report_to_app, #report_to_subscribers
Constructor Details
Instance Method Details
#answer_callback_query(callback_query_id, text: nil, show_alert: false) ⇒ Object
109 110 111 112 113 114 115 |
# File 'lib/flow_chat/telegram/client.rb', line 109 def answer_callback_query(callback_query_id, text: nil, show_alert: false) api_request("answerCallbackQuery", { callback_query_id: callback_query_id, text: text, show_alert: show_alert }.compact) end |
#delete_message(chat_id, message_id) ⇒ Object
127 128 129 130 131 132 |
# File 'lib/flow_chat/telegram/client.rb', line 127 def (chat_id, ) api_request("deleteMessage", { chat_id: chat_id, message_id: }) end |
#delete_webhook ⇒ Object
170 171 172 |
# File 'lib/flow_chat/telegram/client.rb', line 170 def delete_webhook api_request("deleteWebhook") end |
#download_file(file_id) ⇒ Object
Download the raw bytes for an inbound file_id
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/flow_chat/telegram/client.rb', line 198 def download_file(file_id) url = file_url(file_id) return nil unless url uri = URI(url) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true response = http.get(uri.request_uri) if response.is_a?(Net::HTTPSuccess) response.body else FlowChat.logger.error { "Telegram::Client: File download error: #{response.code}" } nil end end |
#edit_message_text(chat_id, message_id, text, keyboard: nil, parse_mode: "HTML") ⇒ Object
117 118 119 120 121 122 123 124 125 |
# File 'lib/flow_chat/telegram/client.rb', line 117 def (chat_id, , text, keyboard: nil, parse_mode: "HTML") api_request("editMessageText", { chat_id: chat_id, message_id: , text: text, parse_mode: parse_mode, reply_markup: keyboard ? {inline_keyboard: keyboard} : nil }.compact) end |
#file_url(file_id) ⇒ Object
Build the download URL for an inbound file_id
188 189 190 191 192 193 194 195 |
# File 'lib/flow_chat/telegram/client.rb', line 188 def file_url(file_id) # get_file answers nil when the API refused, so this cannot assume a # hash back the way it did while every request returned its envelope. file_path = get_file(file_id)&.dig("result", "file_path") return nil unless file_path "https://api.telegram.org/file/bot#{@config.bot_token}/#{file_path}" end |
#get_file(file_id) ⇒ Object
Get file metadata (including file_path) for an inbound file_id
183 184 185 |
# File 'lib/flow_chat/telegram/client.rb', line 183 def get_file(file_id) api_request("getFile", {file_id: file_id}) end |
#get_me ⇒ Object
178 179 180 |
# File 'lib/flow_chat/telegram/client.rb', line 178 def get_me api_request("getMe") end |
#get_webhook_info ⇒ Object
174 175 176 |
# File 'lib/flow_chat/telegram/client.rb', line 174 def get_webhook_info api_request("getWebhookInfo") end |
#indicate_typing(chat_id) ⇒ Hash
Show a typing indicator in a Telegram chat.
Convenience wrapper around send_chat_action(chat_id, action: "typing").
The indicator lasts ~5 seconds or until the next outbound message;
there is no stop-typing call.
157 158 159 |
# File 'lib/flow_chat/telegram/client.rb', line 157 def indicate_typing(chat_id) send_chat_action(chat_id, action: "typing") end |
#send_audio(chat_id, audio_url_or_id, caption: nil) ⇒ Object
94 95 96 97 98 99 100 |
# File 'lib/flow_chat/telegram/client.rb', line 94 def send_audio(chat_id, audio_url_or_id, caption: nil) api_request("sendAudio", { chat_id: chat_id, audio: audio_url_or_id, caption: caption }.compact) end |
#send_chat_action(chat_id, action: "typing") ⇒ Hash
Send a chat action (e.g. typing indicator) to a Telegram chat.
The action lasts ~5 seconds or until the next outbound message. Valid actions per Telegram Bot API: "typing", "upload_photo", "record_video", "upload_video", "record_voice", "upload_voice", "upload_document", "choose_sticker", "find_location", "record_video_note", "upload_video_note".
145 146 147 |
# File 'lib/flow_chat/telegram/client.rb', line 145 def send_chat_action(chat_id, action: "typing") api_request("sendChatAction", chat_id: chat_id, action: action) end |
#send_document(chat_id, document_url_or_id, caption: nil) ⇒ Object
78 79 80 81 82 83 84 |
# File 'lib/flow_chat/telegram/client.rb', line 78 def send_document(chat_id, document_url_or_id, caption: nil) api_request("sendDocument", { chat_id: chat_id, document: document_url_or_id, caption: caption }.compact) end |
#send_message(chat_id, prompt, choices: nil, media: nil) ⇒ Object
Main send_message method matching FlowChat pattern
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/flow_chat/telegram/client.rb', line 16 def (chat_id, prompt, choices: nil, media: nil) FlowChat.logger.info { "Telegram::Client: Sending message to chat #{chat_id}" } response = FlowChat::Telegram::Renderer.new(prompt, choices: choices, media: media).render type, content, = response case type when :text send_text(chat_id, content) when :inline_keyboard send_text_with_keyboard(chat_id, content, [:keyboard]) when :photo send_photo(chat_id, [:url], caption: content) when :photo_with_keyboard send_photo_with_keyboard(chat_id, [:url], caption: content, keyboard: [:keyboard]) when :document send_document(chat_id, [:url], caption: content) when :video send_video(chat_id, [:url], caption: content) when :audio send_audio(chat_id, [:url], caption: content) when :voice send_voice(chat_id, [:url]) else send_text(chat_id, content.to_s) end end |
#send_photo(chat_id, photo_url_or_id, caption: nil) ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/flow_chat/telegram/client.rb', line 61 def send_photo(chat_id, photo_url_or_id, caption: nil) api_request("sendPhoto", { chat_id: chat_id, photo: photo_url_or_id, caption: caption }.compact) end |
#send_photo_with_keyboard(chat_id, photo_url_or_id, caption: nil, keyboard: nil) ⇒ Object
69 70 71 72 73 74 75 76 |
# File 'lib/flow_chat/telegram/client.rb', line 69 def send_photo_with_keyboard(chat_id, photo_url_or_id, caption: nil, keyboard: nil) api_request("sendPhoto", { chat_id: chat_id, photo: photo_url_or_id, caption: caption, reply_markup: keyboard ? {inline_keyboard: keyboard} : nil }.compact) end |
#send_text(chat_id, text, parse_mode: "HTML") ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/flow_chat/telegram/client.rb', line 44 def send_text(chat_id, text, parse_mode: "HTML") api_request("sendMessage", { chat_id: chat_id, text: text, parse_mode: parse_mode }.compact) end |
#send_text_with_keyboard(chat_id, text, keyboard, parse_mode: "HTML") ⇒ Object
52 53 54 55 56 57 58 59 |
# File 'lib/flow_chat/telegram/client.rb', line 52 def send_text_with_keyboard(chat_id, text, keyboard, parse_mode: "HTML") api_request("sendMessage", { chat_id: chat_id, text: text, parse_mode: parse_mode, reply_markup: {inline_keyboard: keyboard} }.compact) end |
#send_video(chat_id, video_url_or_id, caption: nil) ⇒ Object
86 87 88 89 90 91 92 |
# File 'lib/flow_chat/telegram/client.rb', line 86 def send_video(chat_id, video_url_or_id, caption: nil) api_request("sendVideo", { chat_id: chat_id, video: video_url_or_id, caption: caption }.compact) end |
#send_voice(chat_id, voice_url_or_id) ⇒ Object
102 103 104 105 106 107 |
# File 'lib/flow_chat/telegram/client.rb', line 102 def send_voice(chat_id, voice_url_or_id) api_request("sendVoice", { chat_id: chat_id, voice: voice_url_or_id }) end |
#set_webhook(url, secret_token: nil, allowed_updates: nil) ⇒ Object
Webhook management
162 163 164 165 166 167 168 |
# File 'lib/flow_chat/telegram/client.rb', line 162 def set_webhook(url, secret_token: nil, allowed_updates: nil) api_request("setWebhook", { url: url, secret_token: secret_token, allowed_updates: allowed_updates || ["message", "callback_query"] }.compact) end |