chat_sdk-x 0.3.1 → 0.5.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 +4 -4
- data/lib/chat_sdk/x/adapter.rb +34 -1
- data/lib/chat_sdk/x/api_client.rb +10 -0
- data/lib/chat_sdk/x/event_parser.rb +27 -0
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: df8df52963fbcdfc02f6457a2470c9708aa276d08443d5ec55a2174194db06c3
|
|
4
|
+
data.tar.gz: 012ca08f40e7e0b86638879fdc4db880ec46e43f95e3e6b7632bd51212872db2
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 9762fc0118566399c42d729c675246cb326548083d8d8ed3d930503f892a450ad0cb64a026af4c573ebbad7873137a52df32e667a68d02a975e2981f275d9d62
|
|
7
|
+
data.tar.gz: 77e54b2060e5ecdc190466eaf666d4a7ed20db53ee7ca382d46b2a99f87ade839b00ab5084cb9b4b8827a84a77d9fc0296990bb33991678493a63f990c4589dc
|
data/lib/chat_sdk/x/adapter.rb
CHANGED
|
@@ -7,7 +7,7 @@ require "rack/utils"
|
|
|
7
7
|
module ChatSDK
|
|
8
8
|
module X
|
|
9
9
|
class Adapter < ChatSDK::Adapter::Base
|
|
10
|
-
capabilities :direct_messages, :reactions
|
|
10
|
+
capabilities :direct_messages, :reactions, :delete_messages, :message_history
|
|
11
11
|
|
|
12
12
|
attr_reader :client
|
|
13
13
|
|
|
@@ -100,6 +100,39 @@ module ChatSDK
|
|
|
100
100
|
end
|
|
101
101
|
end
|
|
102
102
|
|
|
103
|
+
def delete_message(channel_id:, message_id:) # rubocop:disable Lint/UnusedMethodArgument
|
|
104
|
+
@client.delete_tweet(tweet_id: message_id)
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def fetch_messages(channel_id:, thread_id: nil, cursor: nil, limit: 50) # rubocop:disable Lint/UnusedMethodArgument
|
|
108
|
+
if thread_id&.start_with?("x:dm:")
|
|
109
|
+
participant_id = thread_id.delete_prefix("x:dm:")
|
|
110
|
+
result = @client.get_dm_events(participant_id: participant_id, cursor: cursor, limit: limit)
|
|
111
|
+
data = result["data"] || []
|
|
112
|
+
messages = data.map do |dm|
|
|
113
|
+
ChatSDK::Message.new(
|
|
114
|
+
id: dm["id"]&.to_s,
|
|
115
|
+
text: dm["text"] || "",
|
|
116
|
+
author: ChatSDK::Author.new(
|
|
117
|
+
id: dm["sender_id"] || "unknown",
|
|
118
|
+
name: dm["sender_id"] || "unknown",
|
|
119
|
+
platform: :x,
|
|
120
|
+
bot: false
|
|
121
|
+
),
|
|
122
|
+
thread_id: thread_id,
|
|
123
|
+
channel_id: channel_id,
|
|
124
|
+
platform: :x,
|
|
125
|
+
raw: dm
|
|
126
|
+
)
|
|
127
|
+
end
|
|
128
|
+
next_cursor = result.dig("meta", "next_token")
|
|
129
|
+
[messages, next_cursor]
|
|
130
|
+
else
|
|
131
|
+
# X does not provide a robust thread-fetching API for tweets
|
|
132
|
+
[[], nil]
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
|
|
103
136
|
def add_reaction(channel_id:, message_id:, emoji:) # rubocop:disable Lint/UnusedMethodArgument
|
|
104
137
|
@client.like_tweet(user_id: @user_id, tweet_id: message_id)
|
|
105
138
|
end
|
|
@@ -27,6 +27,16 @@ module ChatSDK
|
|
|
27
27
|
request(:delete, "/2/users/#{user_id}/likes/#{tweet_id}")
|
|
28
28
|
end
|
|
29
29
|
|
|
30
|
+
def delete_tweet(tweet_id:)
|
|
31
|
+
request(:delete, "/2/tweets/#{tweet_id}")
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def get_dm_events(participant_id:, cursor: nil, limit: 50)
|
|
35
|
+
query = "max_results=#{[limit, 100].min}&dm_event.fields=id,text,sender_id,created_at"
|
|
36
|
+
query += "&pagination_token=#{cursor}" if cursor
|
|
37
|
+
request(:get, "/2/dm_conversations/with/#{participant_id}/dm_events?#{query}")
|
|
38
|
+
end
|
|
39
|
+
|
|
30
40
|
private
|
|
31
41
|
|
|
32
42
|
def base_url
|
|
@@ -10,6 +10,7 @@ module ChatSDK
|
|
|
10
10
|
events = []
|
|
11
11
|
events.concat(parse_mentions(payload, bot_user_id))
|
|
12
12
|
events.concat(parse_direct_messages(payload, bot_user_id))
|
|
13
|
+
events.concat(parse_favorites(payload, bot_user_id))
|
|
13
14
|
events
|
|
14
15
|
end
|
|
15
16
|
|
|
@@ -81,6 +82,32 @@ module ChatSDK
|
|
|
81
82
|
)
|
|
82
83
|
end
|
|
83
84
|
end
|
|
85
|
+
|
|
86
|
+
def parse_favorites(payload, bot_user_id)
|
|
87
|
+
fav_events = payload["favorite_events"] || []
|
|
88
|
+
return [] unless fav_events.is_a?(Array)
|
|
89
|
+
|
|
90
|
+
fav_events.filter_map do |data|
|
|
91
|
+
user = data["user"] || {}
|
|
92
|
+
user_id = user["id_str"] || user["id"]&.to_s
|
|
93
|
+
next if bot_user_id && user_id == bot_user_id
|
|
94
|
+
|
|
95
|
+
tweet = data["favorited_status"] || {}
|
|
96
|
+
tweet_id = tweet["id_str"] || tweet["id"]&.to_s
|
|
97
|
+
|
|
98
|
+
ChatSDK::Events::Reaction.new(
|
|
99
|
+
emoji: "heart",
|
|
100
|
+
user_id: user_id || "unknown",
|
|
101
|
+
message_id: tweet_id || "unknown",
|
|
102
|
+
thread_id: "x:post:#{tweet_id}",
|
|
103
|
+
channel_id: user_id,
|
|
104
|
+
added: true,
|
|
105
|
+
platform: :x,
|
|
106
|
+
adapter_name: :x,
|
|
107
|
+
raw: data
|
|
108
|
+
)
|
|
109
|
+
end
|
|
110
|
+
end
|
|
84
111
|
end
|
|
85
112
|
end
|
|
86
113
|
end
|