Discord Requests

Alright I’ll still be looking at this topic.

Also, meanwhile apache2 decides to work for once. Why are you using this?
As you can see on the comment it is unsecure, so I am warning you since this means anyone could send a message with your robot.

It will be a private use one so there won’t be any risk.

Then, as long as they don’t discover your server’s IP/Domain it’s fine

Sup, I did it. So I opened a temporal web app at (closed) * I updated the code to this:

let Https = require("https");
let Fs = require("fs");

let myServer = new Https.Server({
    cert: Fs.readFileSync(*),
    key: Fs.readFileSync(*)
});
myServer.on("request", function(request, response){  
    request.on("data", function(chunk){
        if(!request.body) request.body = chunk;
        else request.body += chunk;
    });

    request.on("end", function() {
        request.bodyEnded = true;
    });

    request.getBody = function(caller){
        if(request.bodyEnded) caller(request.body);
        else {
            request.on("end", function(){
                caller(request.body);
            });
        };
    };

    /*
        So let me explain, here we are creating a custom property called "body" in request. This will be the body that is sent if
        something like POST is used.
        ALWAYS use getBody, it will yield until a body is returned.
    */

    /*
        Put your shenanigans here
        Tip: For returning values use response.writeHead, response.write and response.end()
    */

    console.log(request.headers);
    request.getBody(function(body){
        console.log(body);
    });

    response.writeHead(200, "OK."); //We are saying that everything was fine
    response.write("Very nice"); //This is the body that will be returned
    response.end(); //Ending the request
});

myServer.listen(3000);

/*

NOTE: THIS SERVER IS UNSECURE.

THIS SERVER IS RUNNING ON HTTP and not HTTPS, HTTPS IS THE SECURE METHOD. To use HTTPS however, you need a certificate, key and a bunch of
other stuff.

Another note: You also need a domain. Roblox does not allow secure connections via an IP. You need to use a domain server
that redirects to the port 3000. So something like mydomain.com/api will redirect to the port 3000

If you don't want to deal with these stuff you don't need to. Just be aware that people can check what you are sending to the
servers. So don't send passwords or those things.
*/

You can test it using the following roblox code:

local HttpService = game:GetService("HttpService");
print("Result:", HttpService:RequestAsync({
	Url = *,
	Method = "POST",

	Headers = {
		IAmAHeader = "nice"
	},

	Body = "very nice!!!"
}));

Result:
image

You should be able to use the new Open Cloud feature that roblox provides. Just setup an API key on the create dashboard, and you can send requests to Roblox’s open cloud with that API key, and listen for the MessagingService in your game, like so:

local MessagingService = game:GetService("MessagingService")

MessagingService:SubscribeAsync("HelloWorld", function(Data)
	print("HelloWorld was fired externally with data: "..Data)
end)

And your JavaScript code on the server:

let Response = await fetch("https://apis.roblox.com/messaging-service/v1/universes/YOURUNIVERSEID/topics/HelloWorld", {
	method: "POST",
	headers: {
		"x-api-key": "YOURAPIKEY",
		"Content-Type": "application/json"
	},
	body: JSON.stringify({
		"message": "Your data here!"
	})
})

This solution is much simpler, faster, and much more secure.

For the new Https.Server({}) what would I put for the cert and key?

How would I sent it from a Discord Bot though

You don’t use HTTPS. Use the normal HTTP code that I sent you before. What I sent you is just what I put in the node script on my server.

You can use the node-fetch npm module, and send a POST request to https://apis.roblox.com/messaging-service/v1/universes/YOURUNIVERSEID/topics/EVENTNAME with your Open Cloud API key and data

@typechecked has a very interesting solution. For this case you need to do it the other way, so you use Long-term poling from the roblox place to the node script.

That just means: Loop a bunch of times until you get a response.

So instead of the place sending a request to the server, it is the server that keeps sending a request each like 1 second to the place.

If the place has a pending “message” it will send it to the server as a response and then the server can use that message and post it.

Alternatively you can do both. Make it so that the place sends an unsecure request to the server that says “Check my data”. Then the server sends a request back to the place using the secure roblox method and then posts the message.

For anyone reading: I was wrong, check the replies for further information (I thought he needed the server to be up-to-date).

Will the http server be my IP then?

That is incorrect. My solution uses Roblox’s open cloud, and does not touch web sockets or long-term polling at all. It uses Roblox’s own APIs, and is much simpler than what you describe.

Yes, so in the roblox code you do:

local HttpService = game:GetService("HttpService");
print("Result:", HttpService:RequestAsync({
	Url = "http://111.111.111.111:3000",
	Headers = {
		IAmAHeader = "nice"
	},

	Body = "very nice!!!",
	
	Method = "POST"
}));

Then please enlight me. How would the server be up-to-date? I thought MessageService was a one way thing.

I don’t believe that’s what the OP requires, does he? I thought the OP just needed to send an event to the game from a Discord bot, and have the game act accordingly.

Just getting a connect fail error

Here’s my code
image

I see, I just checked, and you are right. @FireManGuy4321 use his method if you don’t require the server to be up-to-date and only send a message. I read the topic wrong!

1 Like

Can you show me your node code? Did you run the node script?

Note: Try to run the script empty, without anything else for now. Then we will implement the code on the bot