Conversation started Dec 30, 2020 at 14:32.
Dec 30, 2020 14:32
Hey everyone. Got a small question. Is it possible to approach TIO through an API or maybe CURL or something like that? Reason I'm asking is because I'm interested in making a chat bot where users can send small code snippets towards it and get the result returned :)
Dec 30, 2020 15:20
@icecub Short answer: yes but officially no. Long answer: it's possible to interact with TIO via a program (e.g. Python's requests module), but it's not the easiest to do. This is a function I wrote in Python that takes a piece of Jelly code, some arguments and the contents of STDIN to be fed to TIO that does return the intended result
I'll be happy to help you adapt/change that code to fit your needs, just drop me a ping
Also, I haven't used that code in about 18 months, but since no new dev has happened in the last year, the only reason it should fail is because something in TIO's API has been depreciated/broken/removed etc.
@cairdcoinheringaahing Oof. Thanks for the reply, but I think that'll be challenging. I have zero experience with Python. My idea was for a Discord bot written in PHP. Hence the request for CURL or an API like some JSON package. If you could help with that though, I'm definitely interested. I do know a lot about PHP :)
@icecub PHP isn't my strong suit, but I can work you through the API request in language-non-specific terms
So long as PHP has some way of compressing via zlib
Ye that would be great. Honestly, all I need is pretty basic. Just send a request to the server with whatever data is required (langauge, code) and get the result returned. And ye, PHP can compress with zlib
Ok, so it's basically just a POST request to a URL using a zlib compressed query
The basics out of the way: the URL is https://tio.run/cgi-bin/run/api/
Alright, sounds easy enough. When I checked the source, it seems to also work with a token. Do I need to worry about that?
Dec 30, 2020 15:29
@icecub I don't think so, but I'll check what the verify and cookies arguments in my code are for after I go through the query
Alright
So the query is a bunch of segments split up by null bytes. Each bit that's {...} should be replaced with the actual thing when sending the query (e.g. in abc{language}def, {language} should be replaced by the name of the language on TIO)
 Vlang\u00001\u0000{language}\u0000F.code.tio\u0000{# of bytes in code}\u0000{code}F.input.tio\u0000{length of input}\u0000{input}Vargs\u0000{number of ARGV}{ARGV}\u0000R
Ah. Ye that seems clear enough :)
For example, running this would have a query of Vlang\x001\x00jelly\x00F.code.tio\x001\x00+F.input.tio\x000\x00Vargs\x001\x002\x00R
The {ARGV} bit is slightly more complicated; essentially, you join the ARGV on null bytes first, then replace the {ARGV} bit in the query. This becomes Vlang\x001\x00jelly\x00F.code.tio\x001\x00+F.input.tio\x000\x00Vargs\x002\x002\x001\x00R
Once you've got the query, convert it to bytes then compress it with zlib
Next, send a POST request to the URL with the compressed query. As far as I can tell, the verify and cookies bit of my code didn't actually affect anything, so simply request(URL, compress(QUERY)) should work
Finally, the first 16 characters of the test of the response are the "seperator" between TIO's STDOUT and STDERR. Extract them, then split the response on it and you've got the full response in easy to understand form
@icecub Are you planning on it running for one specific language on TIO or multiple?
@cairdcoinheringaahing Sorry, got a phone call. I'll be back shortly. Planning multple, but mostly just Javascript, PHP and Python. Reasonably small snippets (Discord doesn't allow more than 2k chars per message)
Dec 30, 2020 15:45
@icecub No worries, I'm fine talking into the void :) If you're using multiple languages, make sure you get the name of the language right for each in the query. You can find the TIO name for each by going to tio.run/#, searching for the language and clicking on it. The name is directly after the # in the URL
For example: https://tio.run/#python3-stackless for Stackless Python or https://tio.run/#javascript-node for Javascript (Node)
Hope all this has helped, and good luck with the bot!
@cairdcoinheringaahing Finally back. Thanks a lot for all your help! I'm going to have a look at all the information you provided and see what I can come up it. Truly appreciate it!
 
Conversation ended Dec 30, 2020 at 15:53.