Skip to content

Commit 6030153

Browse files
author
Anonymous
committed
Add in the ability to get channel history correctly
1 parent f66d276 commit 6030153

File tree

3 files changed

+110
-10
lines changed

3 files changed

+110
-10
lines changed

src/Slack.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,27 @@ using HTTP
44
using CodecZlib
55
using JSON
66

7+
# Message object in the threaded form
8+
mutable struct threadedMessage
9+
edited::Bool
10+
ts::String
11+
thread_ts::String
12+
user::String
13+
text::String
14+
end
15+
16+
# A thread is an array of threadMessage's
17+
mutable struct thread
18+
thread::Array{threadedMessage}
19+
end
20+
21+
mutable struct message
22+
edited::Bool
23+
ts::String
24+
user::String
25+
text::String
26+
end
27+
728
include("send.jl")
829
export
930
sendtoslack,

src/history.jl

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,72 @@
22
getchannelhistory(token::String)
33
Takes in the Slack App Auth token and channel ID.
44
5+
Returns the 800 most recent messags (this may not be the case always). Could be time based...
6+
57
Tokens can be accessed at "https://api.slack.com/apps/random-letters-and-numbers/oauth?"
68
"""
79
function getchannelhistory(token::String, channelid::String)
8-
HTTP.setuseragent!("HTTP.jl")
910

11+
HTTP.setuseragent!("HTTP.jl")
1012
url = "https://slack.com/api/conversations.history?token=$(token)&channel=$(channelid)"
11-
1213
r = HTTP.get(url, ["Content-Type" => "application/json"])
1314

1415
json_obj = JSON.parse(String(r.body))
1516
message_holder = []
17+
18+
#TODO: Need to figure out a parameter to constrain the time period in which messages are returned.
19+
1620
for (k,v) in json_obj
1721
if occursin("messages", k)
1822
for item in v
23+
24+
# Declare the holder variables
25+
edited = false
26+
ts = ""
27+
thread_ts = ""
28+
user = ""
29+
text = ""
30+
31+
# println("Starting to parse a new message") #DEBUG
1932
for (key,value) in item
20-
if key == "client_msg_id"
21-
println("client_msg_id: $(value)")
33+
if key == "edited"
34+
edited = true
35+
36+
# TODO: also convert this to Date Time and add that for print out.
37+
elseif key == "ts"
38+
ts = value
39+
2240
elseif key == "text"
23-
println("text: $(value)")
41+
text = value
42+
43+
elseif key == "user"
44+
user = value
45+
46+
# It's a thread not a message...
47+
elseif key == "thread_ts"
48+
thread_ts = value
2449
end
2550
end
51+
52+
# If ts and thread_ts are the same, then this is a thread so call the thread function
53+
if ts == thread_ts && ts != ""
54+
55+
try
56+
thread_msg = getthreads(token, channelid, ts)
57+
push!(message_holder, thread_msg)
58+
catch
59+
println("Failed to create a threaded message object")
60+
end
61+
62+
# Else it's a normal message so create it here and push it into an array.
63+
else
64+
msg = message(edited, ts, user, text)
65+
push!(message_holder, msg)
66+
end
2667
end
2768
end
2869
end
2970

71+
return(message_holder)
72+
3073
end

src/threads.jl

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,66 @@
11
"""
22
getthreads(token::String)
33
Takes in the Slack App Auth token, channel ID, and Time Signature of the parent message.
4+
Return the first 50 replies ina thread.
45
56
https://api.slack.com/methods/conversations.replies#arg_ts
67
"""
78
function getthreads(token::String, channelid::String, ts::String)
89
HTTP.setuseragent!("HTTP.jl")
910

11+
# TODO: 50 is an arbitary number, see if there is a limit.
1012
url = "https://slack.com/api/conversations.replies?token=$(token)&channel=$(channelid)&ts=$(ts)&limit=50"
1113

1214
r = HTTP.get(url, ["Content-Type" => "application/json"])
15+
message_holder = []
1316

1417
json_obj = JSON.parse(String(r.body))
1518
message_holder = []
1619
for (k,v) in json_obj
1720
if occursin("messages", k)
1821
for item in v
19-
for (key,value) in item
20-
if key == "text"
21-
println(value)
22-
elseif key == "user"
23-
println("User with ID $(value) said:")
22+
23+
# Declare the holder variables
24+
edited = false
25+
ts = ""
26+
thread_ts = ""
27+
user = ""
28+
text = ""
29+
30+
# println("Starting to look through next Dict/Object") #DEBUG
31+
for (key, value) in item
32+
if key == "user"
33+
user = value
34+
35+
elseif key == "edited"
36+
edited = true
37+
38+
elseif key == "ts"
39+
ts = value
40+
41+
elseif key == "thread_ts"
42+
thread_ts = value
43+
44+
elseif key == "text"
45+
text = value
2446
end
2547
end
48+
49+
# Create the message object.. Handle a failed parse and object creation error.
50+
try
51+
new_tm = threadedMessage(edited, ts, thread_ts, user, text)
52+
53+
# Push it into the holder array
54+
push!(message_holder, new_tm)
55+
catch
56+
println("Enabled to created a threaded message with the following details: $(edited), $(ts), $(thread_ts), $(user), $(text)")
57+
end
58+
2659
end
2760
end
2861
end
2962

63+
# Create and Return the Thread object (just an array of messages)
64+
return(message_holder)
65+
3066
end

0 commit comments

Comments
 (0)