Skip to content

Instantly share code, notes, and snippets.

@nachokb
Last active Feb 7, 2020
Embed
What would you like to do?
CloudFlare workers test
curl 'https://hooks.slack.com/services/xyz/xyz/xyz' -X POST -H 'Content-Type: text/plain;charset=UTF-8' --compressed
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