tlk.io bot - web remote control of scripts

Post your working scripts, libraries and tools for AHK v1.1 and older
0x00
Posts: 89
Joined: 22 Jan 2019, 13:12

Re: tlk.io bot - web remote control of scripts

30 May 2019, 16:47

TheDewd wrote:
29 May 2019, 08:04
Sorry, not yet. It's possible to use the API, however the authentication is where I'm stuck, I think...
Well that blows, I'm going to try using POST then, I'll 'post' it here when i figure it out.


And It seems i don't really use my browser nearly as well as i should, cheers.
mmoalem
Posts: 5
Joined: 30 Mar 2023, 04:30

Re: tlk.io bot - web remote control of scripts

30 Mar 2023, 08:42

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
	}
}
Hi - this is exactly what I needed for my windows PC that I want to control. Now I have to work out how to post to tlk.io from MacroDroid or Tasker (or similar) as I am trying to automate action according to my phone location... Macrodroid can post HTTP request and has json parser but I have no idea what I need to put in them... any help will be appreciated
mmoalem
Posts: 5
Joined: 30 Mar 2023, 04:30

Re: tlk.io bot - web remote control of scripts

30 Mar 2023, 16:16

@TheDewd
Hi man - could really do with your advice...

My goal is to send commands from my android phone to my pc to run certain batch files (commands sent by MacroDroid on phone). After banging my head against the wall for few days I finally got to work out most of the chain from the info on this thread and specifically the script you have posted - I have autohotkey checking tlk.io for messages and run batch files when triggered. On the phone I had less succses as I am not sure how to string the HTTP request to post a message... I managed to extract the chat_id but am not sure what to do next... in your post you have suggested that you managed to get the API data for tlk.io but only seem to use the GET request and I was hoping you might be able to help with the POST string...

The MacroDroid HTTP request options are:
Request Method - I chose POST
Enter URL - I have entered https://tlk.io/{chat_id}
Than there is an option to enter Query Parameters - have no idea what to put there
Than the Content Body - which I assume is the actual message and I have chose text/plain as Content Type, Text as Content Body and input the message I want to post...

Hope all this info makes sense as it is beyond my limited knowledge

Otherwise I was wondering how did you monitor the web traffic and found out the API and JSON data - I figured out that if I can log what is sent from my browser to the site when I post a message I can copy it into MacroDroid...
User avatar
TheDewd
Posts: 1510
Joined: 19 Dec 2013, 11:16
Location: USA

Re: tlk.io bot - web remote control of scripts

30 Mar 2023, 17:54

I figured out how to set a nickname, but I still don't know how to use the returned data to post a new message. I think I'm too out of my depth on this part...

Code: Select all

#SingleInstance, Force

HttpReq := ComObjCreate("WinHttp.WinHttpRequest.5.1")
HttpReq.Open("POST", "https://tlk.io/api/participant")
HttpReq.SetRequestHeader("Content-Type", "application/json")
HttpReq.Send("{""nickname"":""AutoHotkey1234""}")
HttpReq.WaitForResponse()

MsgBox, % HttpReq.ResponseText
MsgBox, % HttpReq.getAllResponseHeaders()

HttpReq := ComObjCreate("WinHttp.WinHttpRequest.5.1")
HttpReq.Open("POST", "https://tlk.io/api/chats/9275239/messages") ; https://tlk.io/mysupersecretchannelname
HttpReq.SetRequestHeader("Content-Type", "application/json")
HttpReq.Send("{""body"":""AutoHotkey Test"",""expired"":false}")
HttpReq.WaitForResponse()

MsgBox, % HttpReq.ResponseText
MsgBox, % HttpReq.getAllResponseHeaders()
mmoalem
Posts: 5
Joined: 30 Mar 2023, 04:30

Re: tlk.io bot - web remote control of scripts

31 Mar 2023, 04:56

@TheDewd
thanks for trying man - appreciated - might have to change tact as I have no idea how to progress with this method...

Think I can get MacroDroid to send emails - can set email client to allow notifications in Windows and get AutoHotKey to check for notifications as per viewtopic.php?f=76&t=76103
mmoalem
Posts: 5
Joined: 30 Mar 2023, 04:30

Re: tlk.io bot - web remote control of scripts

31 Mar 2023, 06:08

@TheDewd
Just making sure I have not left any stone untruned I tried chatGPT (for the first time) and this is the code it came up with
me - write an HTTP request to post message on tlk.io

chatGPT - Sure! Here's an example of an HTTP request to post a message on tlk.io:

makefile
Copy code
POST https://tlk.io/room_name/messages.json HTTP/1.1
Host: tlk.io
Content-Type: application/json
Content-Length: [length of JSON payload]

{
"body": "Hello, this is my message!",
"nickname": "my_nickname",
"user_id": "my_user_id"
}
In this example, you would need to replace "room_name" with the actual name of the tlk.io room you want to post the message in. The JSON payload contains the message body, your chosen nickname, and your user ID. Make sure to replace "Hello, this is my message!" with the actual message you want to post, and "my_nickname" and "my_user_id" with your chosen nickname and user ID.

Note that you would also need to authenticate the request by including a valid API key or access token in the request headers, depending on the authentication method used by tlk.io.


me - write http request that creates a new user on tlk.io in room called london

chatGPT - Unfortunately, tlk.io doesn't offer an API to create new users. Users are created automatically when they join a room for the first time using a unique nickname.

However, if you want to join a room called "london" on tlk.io using a specific nickname, you can send a GET request to the following URL:

arduino
Copy code
https://tlk.io/london?nick=your_nickname
Replace "your_nickname" with the nickname you want to use. This will join the "london" room with the specified nickname. If the nickname is already taken, tlk.io will generate a new one for you.

Note that tlk.io doesn't offer an official API, so this method may not be supported or guaranteed to work in the future.
I have tried to implement it into the autohotkey script but did not work... posting iy just in case it makes more sense to you than me...
User avatar
boiler
Posts: 16902
Joined: 21 Dec 2014, 02:44

Re: tlk.io bot - web remote control of scripts

31 Mar 2023, 06:22

@mmoalem -- Generally, ChatGPT-generated code is not allowed, but since you didn't have it attempt to generate AHK code, which it is consistently bad at, we'll let it stand this time. Just be aware for the future.

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 63 guests