chat_sdk-telegram 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 60abf34d37d9ea64b17959fb9a53264cc3d1ab67407cd1b05e4c5ece105137a0
4
- data.tar.gz: 9e41b3269dc1116e2bc7169b146770337e8f9bbb6020b85f7f36c575828cf678
3
+ metadata.gz: 8a2c77efea728df0e0f77552d2d709d7824f653e2137ae88678ddd7baa6b2415
4
+ data.tar.gz: 51854b5c55aacf5504e16a70e0f5120e188800572cd4da6eecc5bbd4ec205e28
5
5
  SHA512:
6
- metadata.gz: 6dc0d5c433b711162a3a5cb8e66e0604686f254bafe906a8c7a11cebc0d4526d9427800e50a752cbc55ba95e77714d66ba7c4a7accb5f43e7732f86db68f1ac2
7
- data.tar.gz: 9843eee8e48fab1a345487035beac68a014bc558e343346dc1cfe6a170f7799b9170f9cff2e10d9bc8fdb738c4cc73fa118731558508baf8970509e54aeea3e2
6
+ metadata.gz: d222b99e2557c94c16465cff57e2858fb9aba5eda536446fee542502539414f76e9a55485b4706f5c8cedee6662325c7eb20c49e91059a37f67bc5a6a47c4de2
7
+ data.tar.gz: af031c32d6518ac316888941ebc34a216ad05ec3d847cbdee2a3e9cd7418d66157213a59cf0d29616f99c719990a3cb04aadfa441d8d94df3ed163ec7cd18090
@@ -6,14 +6,21 @@ module ChatSDK
6
6
  module Telegram
7
7
  class Adapter < ChatSDK::Adapter::Base
8
8
  capabilities :edit_messages, :delete_messages, :reactions, :file_uploads,
9
- :typing_indicator, :streaming_edit, :direct_messages
9
+ :typing_indicator, :streaming_edit, :direct_messages, :replies
10
10
 
11
11
  attr_reader :client
12
12
 
13
- def initialize(bot_token: nil, secret_token: nil, bot_username: nil)
13
+ def initialize(bot_token: nil, secret_token: nil, bot_username: nil, allowed_user_ids: nil, allow_unverified_webhooks: nil)
14
14
  @bot_token = bot_token || ENV["TELEGRAM_BOT_TOKEN"]
15
15
  @secret_token = secret_token || ENV["TELEGRAM_WEBHOOK_SECRET_TOKEN"]
16
16
  @bot_username = bot_username || ENV["TELEGRAM_BOT_USERNAME"]
17
+ configured_user_ids = allowed_user_ids || ENV["TELEGRAM_ALLOWED_USER_IDS"]&.split(",")
18
+ @allowed_user_ids = configured_user_ids&.map(&:to_s)&.to_set
19
+ @allow_unverified_webhooks = if allow_unverified_webhooks.nil?
20
+ ENV["TELEGRAM_ALLOW_UNVERIFIED_WEBHOOKS"] == "true"
21
+ else
22
+ allow_unverified_webhooks
23
+ end
17
24
 
18
25
  raise ChatSDK::ConfigurationError, "Telegram bot_token required" unless @bot_token
19
26
 
@@ -27,7 +34,12 @@ module ChatSDK
27
34
 
28
35
  # Inbound
29
36
  def verify_request!(rack_request)
30
- return true unless @secret_token
37
+ return true if !@secret_token && @allow_unverified_webhooks
38
+
39
+ unless @secret_token
40
+ raise ChatSDK::SignatureVerificationError,
41
+ "Telegram secret_token required; set allow_unverified_webhooks: true to explicitly disable verification"
42
+ end
31
43
 
32
44
  header_token = rack_request.get_header("HTTP_X_TELEGRAM_BOT_API_SECRET_TOKEN")
33
45
 
@@ -48,6 +60,8 @@ module ChatSDK
48
60
 
49
61
  def parse_events(rack_request)
50
62
  payload = read_json_body(rack_request)
63
+ return [] unless allowed_user?(payload)
64
+
51
65
  EventParser.parse(payload, bot_username: @bot_username)
52
66
  rescue JSON::ParserError
53
67
  []
@@ -67,6 +81,18 @@ module ChatSDK
67
81
  parse_telegram_message(result, channel_id)
68
82
  end
69
83
 
84
+ def reply_message(channel_id:, message_id:, message:, thread_id: nil)
85
+ text, reply_markup = prepare_message_payload(message)
86
+ result = @client.send_message(
87
+ chat_id: channel_id,
88
+ text: text,
89
+ reply_markup: reply_markup,
90
+ reply_to_message_id: message_id
91
+ )
92
+
93
+ parse_telegram_message(result, channel_id)
94
+ end
95
+
70
96
  def edit_message(channel_id:, message_id:, message:)
71
97
  text, reply_markup = prepare_message_payload(message)
72
98
 
@@ -150,6 +176,8 @@ module ChatSDK
150
176
  updates = updates.is_a?(Array) ? updates : []
151
177
  updates.each do |update|
152
178
  offset = update["update_id"] + 1
179
+ next unless allowed_user?(update)
180
+
153
181
  events = EventParser.parse(update, bot_username: @bot_username)
154
182
  events.each { |event| block.call(event) }
155
183
  end
@@ -158,6 +186,18 @@ module ChatSDK
158
186
 
159
187
  private
160
188
 
189
+ def allowed_user?(payload)
190
+ return true unless @allowed_user_ids
191
+
192
+ user_id = payload.dig("callback_query", "from", "id") ||
193
+ payload.dig("message_reaction", "user", "id") ||
194
+ payload.dig("message", "from", "id") ||
195
+ payload.dig("edited_message", "from", "id") ||
196
+ payload.dig("channel_post", "from", "id") ||
197
+ payload.dig("edited_channel_post", "from", "id")
198
+ user_id && @allowed_user_ids.include?(user_id.to_s)
199
+ end
200
+
161
201
  def prepare_message_payload(message)
162
202
  msg = ChatSDK::PostableMessage.from(message)
163
203
  text = msg.text || msg.card&.fallback_text || ""
@@ -36,6 +36,7 @@ module ChatSDK
36
36
  thread_id: thread_id,
37
37
  channel_id: chat_id,
38
38
  platform: :telegram,
39
+ reply_to: build_reply_to(message["reply_to_message"], chat_id),
39
40
  raw: message
40
41
  )
41
42
 
@@ -145,7 +146,28 @@ module ChatSDK
145
146
  def mention?(text, bot_username)
146
147
  return false unless bot_username
147
148
 
148
- text.include?("@#{bot_username}")
149
+ /(?<!\w)@#{Regexp.escape(bot_username)}(?![\w-])/i.match?(text)
150
+ end
151
+
152
+ def build_reply_to(reply, chat_id)
153
+ return unless reply
154
+
155
+ user = extract_user(reply)
156
+ ChatSDK::Message.new(
157
+ id: reply["message_id"]&.to_s,
158
+ text: reply["text"] || reply["caption"] || "",
159
+ author: ChatSDK::Author.new(
160
+ id: user[:id],
161
+ name: user[:name],
162
+ platform: :telegram,
163
+ bot: !!reply.dig("from", "is_bot"),
164
+ locale: user[:locale]
165
+ ),
166
+ thread_id: resolve_thread_id(reply),
167
+ channel_id: chat_id,
168
+ platform: :telegram,
169
+ raw: reply
170
+ )
149
171
  end
150
172
 
151
173
  def parse_bot_command(message, user, chat_id, thread_id)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chat_sdk-telegram
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Quentin Rousseau