Email workers

Hello, I was directed here by CF support so I thought I would give it a shot. I am trying to implement an email worker script, when the email rout is hit it forwards to a worker that executes a JavaScript. The have script in question I want to trigger hit a APi and open a ticket. I am not a JavaScript or CF worker expert however I was the JavaScript works local and a ticket is open, however when I try the same script in the Email worker, nothing happens. fetch('https://compant.com.zendesk.com/api/v2/tickets.json', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': 'Basic ' + btoa('<email_address>/token:<api_key>') }, body: JSON.stringify({ ticket: { subject: 'Test ticket', comment: { body: 'This is a test ticket' } } }) }) .then(response => response.json()) .then(data => console.log(data)) .catch(error => console.error(error));
7 Replies
xxjs17xx
xxjs17xx7mo ago
Note this is wrapped in the export default { async email(message, env, ctx) { } } Also note url username and password have been redacted
Chaika
Chaika7mo ago
You're not awaiting it, the worker execution is most likely ending instantly and killing the request. Add await, ex await fetch(....) Then update, and tail it as well. If you're using the dashboard, you can navigate to your Worker, and then Logs -> Start Log Stream, and send an email. If you're using wrangler, use wrangler tail (Point of tailing is you can see the email event and any errors, etc)
xxjs17xx
xxjs17xx7mo ago
I have tried it with await.. the streamed logs say OK but I do get a bunch of fecth handler erros export default { async email(message, env, ctx) { await fetch('https://<redacted>.zendesk.com/api/v2/tickets.json', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Accept':'application/json', 'Authorization': 'Basic ' + btoa('<redacted>/token:<redacted>') }, body: JSON.stringify({ ticket: { subject: 'Test ticket jon sammut', comment: { body: 'This is a test ticket jon sammut' } } }) }) .then(response => response.json()) .then(data => console.log(data)) .catch(error => console.error(error)); } } just a side note this is the new Email workers vs regular workers not sure if there are a differeerence
Chaika
Chaika7mo ago
Ignore the fetch handler errors, you don't have a fetch handler and bots find out about brand new Workers when they get issued an ssl cert, so you see an initial surge of requests that go nowhere The details on the email event are what you want, any logs/errors
xxjs17xx
xxjs17xx7mo ago
ah.. .ok it loolks like it is fireing now !! i swear I did this test earlier. well thank you so much Chaika
Chaika
Chaika7mo ago
It might have taken a second to update or something, if you initially deployed without the await. Workers environment is a bit different then browsers and such, if you don't await and you let the execution end, any hanging promises (like fetch requests) get cancelled. Or maybe it was just something else no problem MeowHeartCloudflare
xxjs17xx
xxjs17xx7mo ago
i defeinatly started without the await, so maybe im a tad bit impatient I really apprecaite you taking the time.