Last active
February 7, 2020 16:54
-
-
Save nachokb/78d2ec397ef318fe6602f798403c2b59 to your computer and use it in GitHub Desktop.
CloudFlare workers test
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
curl 'https://hooks.slack.com/services/xyz/xyz/xyz' -X POST -H 'Content-Type: text/plain;charset=UTF-8' --compressed |
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
addEventListener('fetch', event => { | |
event.respondWith(handleRequest(event.request)) | |
}) | |
/** | |
* Notify slack on new chat | |
* | |
* adapted from https://wemanageyoursite.com/slack-and-tawk-to/ | |
* | |
* @param {Request} request | |
*/ | |
async function handleRequest(req) { | |
var url = 'https://hooks.slack.com/services/xyz/xyz/xyz'; | |
var json = await req.json(); | |
var text = `Tawk.to => ${json.message.text}`; | |
var body = 'payload=' + JSON.stringify({ | |
"text": text, | |
"icon_emoji": ":ghost:", | |
"user_name": "CFWorker", | |
"channel": "#mychannel" | |
}); | |
let headers = new Headers(); | |
headers.append( | |
'Content-Type', | |
'application/json' | |
); | |
console.log('JSON'); | |
console.log(json); | |
console.log('BODY'); | |
console.log(body); | |
await fetch(url, { | |
method: 'POST', | |
headers: headers, | |
body: "some payload" // body | |
}); | |
console.log('TEXT'); | |
console.log(text); | |
return new Response('OK', {status: 200}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment