open.mp forum
Sending message to Discord - Printable Version

+ open.mp forum (https://forum.open.mp)
-- Forum: SA-MP (https://forum.open.mp/forumdisplay.php?fid=3)
--- Forum: Tutorials (https://forum.open.mp/forumdisplay.php?fid=37)
--- Thread: Sending message to Discord (/showthread.php?tid=2354)



Sending message to Discord - Kox - 2023-02-19

Hi,

This tutorial aims to give you a basic example about how to use a webhook to send message from your OpenMP/SA-MP server into a discord channel.

Requirements :

1) Install pawn-requests package on your server
  • Install the package with sampctl or manually

2) Creating a Discord webhook
  1. Create text channel
  2. Click "Edit Channel" =>  Integrations => Create Webhook
  3. Click on the Webhook => Copy Webhook url (save it for later)

3) Send message from server
  1. Add requests include and create a RequestsClient with your webhook url
    PHP Code:
    #include <requests>

    new RequestsClient:client;
    main()
    {
        client RequestsClient("replace with your webhookurl");


  2. Send message to discrod
    PHP Code:
    public OnPlayerConnect(playerid)
    {
        new name[MAX_PLAYER_NAME 1];
        GetPlayerName(playeridnamesizeof(name));
        new string[MAX_PLAYER_NAME 23 1];
        format(stringsizeof(string), "%s has joined the server."name);
        RequestJSON(
            client,
            "",            
            HTTP_METHOD_POST

            "OnPostJson",    
            JsonObject
    (
                "content"JsonString(string)
            )
        );
        return 1;
    }

    forward OnPostJson(Request:idE_HTTP_STATUS:statusNode:node);
    public 
    OnPostJson(Request:idE_HTTP_STATUS:statusNode:node
    {
        if(status == HTTP_STATUS_NO_CONTENT) {
            printf("successfully posted message");
        }
        else 
        {
            printf("failed to post message");
        }


Now when a player connect to the server, a message will be sent on the discord channel.

[Image: webhook-discord.png]

Credits
Southclaws for the requests package


Regards


RE: Sending message to Discord - swansonespinoza7507112 - 2023-11-04

It's great that you shared how to use a webhook to send a message from a machine to a discord channel. Thanks to your sharing, I have a better understanding of the uses