TheDewd wrote: ↑21 May 2019, 13:55
You should use the JSON data from the API. I discovered the API data by monitoring the data transfer as I interacted with the website.
Here's a script I wrote to get the latest message text from a channel.
Code: Select all
#SingleInstance, Force
#Persistent
;// The name assigned to the channel
ChannelName := "hey"
;// Get the unique chat_id value using the channel name
HttpRequest := ComObjCreate("WinHttp.WinHttpRequest.5.1")
HttpRequest.Open("GET", "https://tlk.io/" ChannelName, true)
HttpRequest.Send()
HttpRequest.WaitForResponse()
RegExMatch(HttpRequest.ResponseText, "chat_id\s=\s'(\d+)';", ChatID)
;// Get the JSON data returned from the API URL
HttpRequest := ComObjCreate("WinHttp.WinHttpRequest.5.1")
HttpRequest.Open("GET", "https://tlk.io/api/chats/" ChatID1 "/messages", true)
HttpRequest.Send()
HttpRequest.WaitForResponse()
;// Parse the JSON data
ChatLog := [{}] ;// Create an array containing an object
Pos := 1, Match := ""
While (Pos := RegExMatch(HttpRequest.ResponseText, "({.*?})", Match, Pos + StrLen(Match))) {
RegExMatch(Match1, """id"":(\d+),", id)
RegExMatch(Match1, """user_token"":""(.*?)"",", user_token)
RegExMatch(Match1, """nickname"":""(.*?)"",", nickname)
RegExMatch(Match1, """body"":""(.*?)"",", body)
RegExMatch(Match1, """user"":{(.*?)},", user)
RegExMatch(user1, """token"":""(.*?)"",", u_token)
RegExMatch(user1, """nickname"":""(.*?)"",", u_nickname)
RegExMatch(user1, """provider"":""(.*?)"",", u_provider)
RegExMatch(user1, """avatar"":""(.*?)"",", u_avatar)
RegExMatch(user1, """profile_url"":""(.*?)""}", u_profile_url)
RegExMatch(Match1, """timestamp"":(\d+),", timestamp)
RegExMatch(Match1, """closest_milestone"":(\d+),", closest_milestone)
RegExMatch(Match1, """deleted"":(.*?)}", deleted)
;// Assign parsed items to the array
ChatLog[A_Index] := {"id": id1
,"user_token": user_token1
,"nickname": nickname1
,"body": body1
,"u_token": u_token1
,"u_nickname": u_nickname1
,"u_provider": u_provider1
,"u_avatar": u_avatar1
,"u_profile_url": u_profile_url1
,"timestamp": timestamp1
,"closest_milestone": closest_milestone1
,"deleted": deleted1}
}
MsgBox, % ChatLog[ChatLog.MaxIndex()].body
All of the returned JSON attributes are included in the script.
Note: Only the last 50 messages are available to view. If you don't see any results, see if your channel's messages have been deleted...
Login using Facebook/Twitter, and then become the moderator of the channel, and the messages aren't automatically deleted (as far as I can tell...)
You can setup the script to wait for commands like this:
Code: Select all
#SingleInstance, Force
#Persistent
MyChatID := GetChatID("YourChannelName") ;// The name assigned to the channel
SetTimer, ParseChatLog, 3000 ;// 3 seconds
return
GetChatID(Channel) { ;// Get the unique chat_id value using the channel name
HttpRequest := ComObjCreate("WinHttp.WinHttpRequest.5.1")
HttpRequest.Open("GET", "https://tlk.io/" Channel, true)
HttpRequest.Send()
HttpRequest.WaitForResponse()
RegExMatch(HttpRequest.ResponseText, "chat_id\s=\s'(\d+)';", ChatID)
return ChatID1
}
GetChatLog(ChatID) { ;// Get the JSON data returned from the API URL
HttpRequest := ComObjCreate("WinHttp.WinHttpRequest.5.1")
HttpRequest.Open("GET", "https://tlk.io/api/chats/" ChatID "/messages", true)
HttpRequest.Send()
HttpRequest.WaitForResponse()
return HttpRequest.ResponseText
}
ParseChatLog() { ;// Parse the JSON data
Global MyChatID
Static LastID
MyChatLog := GetChatLog(MyChatID) ;// Get the JSON data
ChatLog := [{}] ;// Create an array containing an object
Pos := 1, Match := ""
While (Pos := RegExMatch(MyChatLog, "({.*?})", Match, Pos + StrLen(Match))) {
RegExMatch(Match1, """id"":(\d+),", id)
RegExMatch(Match1, """user_token"":""(.*?)"",", user_token)
RegExMatch(Match1, """nickname"":""(.*?)"",", nickname)
RegExMatch(Match1, """body"":""(.*?)"",", body)
RegExMatch(Match1, """user"":{(.*?)},", user)
RegExMatch(user1, """token"":""(.*?)"",", u_token)
RegExMatch(user1, """nickname"":""(.*?)"",", u_nickname)
RegExMatch(user1, """provider"":""(.*?)"",", u_provider)
RegExMatch(user1, """avatar"":""(.*?)"",", u_avatar)
RegExMatch(user1, """profile_url"":""(.*?)""}", u_profile_url)
RegExMatch(Match1, """timestamp"":(\d+),", timestamp)
RegExMatch(Match1, """closest_milestone"":(\d+),", closest_milestone)
RegExMatch(Match1, """deleted"":(.*?)}", deleted)
;// Assign parsed items to the array
ChatLog[A_Index] := {"id": id1
,"user_token": user_token1
,"nickname": nickname1
,"body": body1
,"u_token": u_token1
,"u_nickname": u_nickname1
,"u_provider": u_provider1
,"u_avatar": u_avatar1
,"u_profile_url": u_profile_url1
,"timestamp": timestamp1
,"closest_milestone": closest_milestone1
,"deleted": deleted1}
}
;// Do nothing if no new messages
If (LastID = ChatLog[ChatLog.MaxIndex()].id) {
return
}
;// Store ID of last message to variable
LastID := ChatLog[ChatLog.MaxIndex()].id
;// Execute command based on the message text
ExecuteCommand(ChatLog[ChatLog.MaxIndex()].body)
}
ExecuteCommand(Text) {
If (Text = "MsgBox") { ;// Keyword 1
MsgBox, % Hello World! ;// Display message box
} Else If (Text = "Start") { ;// Keyword 2
Send, {LWin} ;// Open start menu
} Else If (Text = "Notepad") { ;// Keyword 3
Run, notepad.exe ;// Run notepad
} Else { ;// Other
If (Text = "") {
return ;// Do nothing if no text (messages deleted, etc...)
}
MsgBox, % Text ;// Show text if not matching defined keywords
}
}