Last active
June 16, 2017 19:05
-
-
Save thoughtpolice/06a409a99d92485c8e770ccc798dacf4 to your computer and use it in GitHub Desktop.
Discord BuckleScript Cloud Functions OMG
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Generated by BUCKLESCRIPT VERSION 1.7.5, PLEASE EDIT WITH CARE | |
'use strict'; | |
var Request_promise = require("request-promise"); | |
var Config = require("../../config.json"); | |
function main(ev) { | |
var match = ev.data.data; | |
if (match !== undefined) { | |
if (match !== "") { | |
var msg = Buffer.from(match, "base64").toString(); | |
console.log("Got a message; sending to Discord..."); | |
return Request_promise({ | |
method: "POST", | |
uri: Config.discord_webhook_url, | |
json: true, | |
body: { | |
content: msg, | |
username: "Gomez", | |
tts: false | |
} | |
}); | |
} else { | |
console.log("Not sending a Discord message (PubSub message was null)"); | |
return Promise.resolve(); | |
} | |
} else { | |
console.log("Not sending a Discord message (PubSub message was null)"); | |
return Promise.resolve(); | |
} | |
} | |
exports.main = main; | |
/* request-promise Not a pure module */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
external buffer_from : string -> string -> < toString : unit -> string [@bs.meth] > Js.t | |
= "from" [@@bs.val] [@@bs.scope "Buffer"] | |
external promise_empty : unit -> unit Js.Promise.t | |
= "resolve" [@@bs.val] [@@bs.scope "Promise"] | |
external request_promise : < .. > Js.t -> unit Js.Promise.t | |
= "request-promise" [@@bs.module] | |
external config : < .. > Js.t = "../../config.json" [@@bs.module] | |
let main ev = | |
let open Js in | |
(* TODO FIXME: null check *) | |
match Undefined.to_opt ev##data##data with | |
| Some m when m <> "" -> | |
let msg = (buffer_from m "base64")##toString () in | |
log "Got a message; sending to Discord..."; | |
request_promise [%bs.obj { | |
_method = "POST"; | |
uri = config##discord_webhook_url; | |
json = Js.true_; | |
body = { | |
content = msg; | |
username = "Gomez"; | |
tts = Js.false_; | |
} | |
}]; | |
| _ -> | |
log "Not sending a Discord message (PubSub message was null)"; | |
promise_empty (); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment