0

Currently i'm trying to send a webhook request to discord, so i can send updates to my repository to a channel in my discord server.

If I load the page in my browser, it will run this code just fine, and the message will appear in my discord:

      <script>
      $(document).ready(function(){
        setTimeout(send(), 300);
      });
      function send(){
        var link = "discordwebhooklink";
        var hookurl = link + "/slack";
        var msgJson
        msgJson = {
         "username": "",
         "icon_url": "",
         "text": "Hey! I've got a new project [here](http://repo.mydomain.ca/<?php echo $link; ?>/)!",
         "attachments":[{
           "author_icon": "",
           "author_name": "",
           "color": "#ff9900",
           "fields": [{
            "title": "Repo Project Created!",
            "value": "\n----------------------------------------------------------------------------\n```json\nProject Information\n\nTitle:\n \"<?php echo $title; ?>\"\nDescription:\n \"<?php echo $description; ?>\"\nVersion: <?php echo $version; ?>\n```",
           }]
          }]
        };
        post(hookurl, msgJson);
      }
      </script>

      <script>
      function post(url, jsonmsg){
        xhr = new XMLHttpRequest();
        xhr.open("POST", url, true);
        xhr.setRequestHeader("Content-type", "application/json");
        var data = JSON.stringify(jsonmsg);
        console.log(jsonmsg);
        console.log(data);
        xhr.send(data);
        xhr.onreadystatechange = function() {
            if(this.status != 200){
                alert(this.responseText);
            }
        }
      }
      </script>

But if i run the page from crontab with php /path/to/file.php then it doesn't show up in the channel. I've figured out that the cause is that it won't execute javascript, so I'm here asking, how can I run the PHP code, and Javascript code, from bash. (Note: jQuery is included in the outputted html from the php script)

I also tried doing a pure PHP solution, but couldn't get the messages to appear in my channel either. I tried a few PHP solutions that used CURL, stream_context_create, and include, but I don't understand any of the solutions given, so it could be a fault on my part.

TL;DR: How do I use crontab to execute a .php file that outputs javascript, and have the outputted javascript be executed, not just printed.

Edit: Flagged as duplicate, but i said i couldn't figure out cURL solutions.

Not_Lazy
  • 69
  • 1
  • 8
  • just make the cron job go to the url – Stannio Feb 08 '17 at 15:03
  • The php is grabbing the $title $description and $version and $link variables for sending to discord. _wait_, did you even read the question? I explained that i couldn't figure out how to make php send the data to discord, and that the only working solution was to send it through javascript, the code of which is scrapped from another site sending data to discord. – Not_Lazy Feb 08 '17 at 15:08
  • My excuses, I did read the question but misunderstood the architecture. The tool to run server-side JavaScript is Node.js (but it isn't a browser and requires a full rewrite of your code). The tool to fake a browser from command line is called "headless browser". Both are mentioned in the linked question. – Álvaro González Feb 08 '17 at 16:14

0 Answers0