• If you need help or want to discuss things, you now can also join us on our Discord Server!
  • A first preview of the unlimited version of SinusBot can be found in the Upcoming Changes thread. A version for Windows will follow, but we don't have a release date, yet.

EN How to use http request

Everlike

Well-Known Member
Contributor
Hello,
for quite a while now, Iam looking forward to manage or at least show “databases” (stored arrays of objects) in a webinterface so it’s easier for me to see some data more clear. Unfortunately nobody could really explain me how to do so and I also couldn’t figure it out by myself.
All I want to do is send data from the webpage to the script, and request data of the script from the webpage.

Here is what I want to do in code, I hope its a bit more clear and maybe you can help me out
JavaScript:
registerPlugin({
    name: 'script <-> webpage communicator',
    version: '0.0.1',
    description: 'Script, trying to communicate with a webpage.',
    author: 'Everlike <[email protected]>',
    enableWeb: true,
    requiredModules: ["http"],
    vars: [
    
    ]
},  function (sinusbot, config) {
  
    var engine = require ('engine');
    var backend = require ('backend');
    var event = require ('event');
    var store = require ('store');
    var http = require ('http');
    
    
    event.on('api:caseOne', ev => {
        engine.log(/*received data from .html file in this case "Aloah"*/);
        });

    event.on('api:caseTwo', ev => {
        var infoTheWebpageWant = "Hey Website, its me, the script";
        //send "Hey Website, its me, the script" to the .html file when requested
        });
    
    });

HTML:
<!DOCTYPE html>
<html>
<head>
<title>A Page to communicate with a sinusbot script!</title>
</head>
<body>
<b>Click the following button to send a variable to the script<b>
 <button onclick="sendDataToScript()">Click me</button>
 
 <br>
 
<b>Click the following button to request some data from the script<b>
 <button onclick="requestDataFromScript()">Click me</button>
 <br>



<script>
function sendDataToScript() {
var word = "aloah";
//send the variable "word" to the script. communicate with script event: "api:caseOne"
};

function requestDataFromScript() {
var someInfo;
//reqest a var(string) from the script and save it to the "someInfo" variable on the website by communicating with the "api:caseTwo" event.
}
</script>

</body>
</html>

Am I on the right track?
If someone has the time to explain this to me, I would be excessively happy!

Greetings Everlike
 
Last edited:

Everlike

Well-Known Member
Contributor
push.
Edited the post, added some code snippets, may this helps, helping me out.
 

irgendwr

no longer active, "retired" staff member
is awesome!
V.I.P.
is uber awesome!
Contributor
Insider
I wrote this at 2 AM and did not test it, may contain bugs.
I'm really tired tbh D:

SinusBot Script:
JavaScript:
// see https://sinusbot.github.io/scripting-docs/#eventeventapieventname
event.on('api:caseOne', ev => {
    // see https://sinusbot.github.io/scripting-docs/#apievent
    engine.log(ev.data().foo);
});

// this is the short form of: ev => {return {something: '...'}}
event.on('api:caseTwo', ev => ({
    something: 'Hello browser, how are you doing?'
}));

JavaScript on the web-page (requires jQuery, but you can very easily rewrite it to work without it using fetch), assuming you're logged in and the webpage is included with the script
JavaScript:
function sendDataToScript(instanceID) {
    $.ajax({
        url: '/api/v1/bot/i/' + instanceID + '/event/caseOne',
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'Authorization': 'bearer ' + window.localStorage.token
        },
        data: JSON.stringify({"foo": "bar"})
    });
}

function requestDataFromScript(instanceID) {
    $.ajax({
        url: '/api/v1/bot/i/' + instanceID + '/event/caseTwo',
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'Authorization': 'bearer ' + window.localStorage.token
        },
        data: '{}'
    }).done(function (data) {
        if (!data || data.length == 0) {
            // no data received
            return
        }
        
        // data is an array of responses
        console.log(data[0].something)
    });
}
 

Everlike

Well-Known Member
Contributor
Thank you very very much @irgendwr!
I´ll try to understand it, but this helped me a lot to understand the process of communicating between the sinusbot script and the webpage!
Huge Thanks!
😻
 
Last edited:
Top