Class: FlowChat::Telegram::Client

Inherits:
Object
  • Object
show all
Includes:
Instrumentation
Defined in:
lib/flow_chat/telegram/client.rb

Instance Method Summary collapse

Methods included from Instrumentation

#instrument, instrument, report_api_error

Constructor Details

#initialize(config) ⇒ Client

Returns a new instance of Client.



10
11
12
13
# File 'lib/flow_chat/telegram/client.rb', line 10

def initialize(config)
  @config = config
  FlowChat.logger.info { "Telegram::Client: Initialized Telegram client" }
end

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 delete_message(chat_id, message_id)
  api_request("deleteMessage", {
    chat_id: chat_id,
    message_id: message_id
  })
end

#delete_webhookObject



170
171
172
# File 'lib/flow_chat/telegram/client.rb', line 170

def delete_webhook
  api_request("deleteWebhook")
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 edit_message_text(chat_id, message_id, text, keyboard: nil, parse_mode: "HTML")
  api_request("editMessageText", {
    chat_id: chat_id,
    message_id: message_id,
    text: text,
    parse_mode: parse_mode,
    reply_markup: keyboard ? {inline_keyboard: keyboard} : nil
  }.compact)
end

#get_meObject



178
179
180
# File 'lib/flow_chat/telegram/client.rb', line 178

def get_me
  api_request("getMe")
end

#get_webhook_infoObject



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.

Parameters:

  • chat_id (Integer, String)

    the target chat id

Returns:

  • (Hash)

    parsed Telegram API response



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".

Parameters:

  • chat_id (Integer, String)

    the target chat id

  • action (String) (defaults to: "typing")

    the chat action to broadcast (default: "typing")

Returns:

  • (Hash)

    parsed Telegram API response



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 send_message(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, options = response

  case type
  when :text
    send_text(chat_id, content)
  when :inline_keyboard
    send_text_with_keyboard(chat_id, content, options[:keyboard])
  when :photo
    send_photo(chat_id, options[:url], caption: content)
  when :photo_with_keyboard
    send_photo_with_keyboard(chat_id, options[:url], caption: content, keyboard: options[:keyboard])
  when :document
    send_document(chat_id, options[:url], caption: content)
  when :video
    send_video(chat_id, options[:url], caption: content)
  when :audio
    send_audio(chat_id, options[:url], caption: content)
  when :voice
    send_voice(chat_id, options[: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