Sync and Watch Contract Events With Trigger & Email

Hi there!

Could you perhaps help me understand better how to execute cloud function for contract events, specifically to read from that event and then send an email to registered users based off of that event.

Say I have two users (Bob and Alice). Both of them are in the user table with their emails. Bob buys and item from Alice and event is triggered indicating both of their addresses. I would like to send an email to Alice that her item was purchased. How do I do that?

I understand that step 1 is to get a plugin to sync and watch specific event (web3 -> plugins) - there is a great video explaining that so that’s not a problem.

Step 2 is to add afterSave trigger in cloud function, which is where I’m stuck.

I’m sure that what’s in the docs is really good for those with more experience however I am stuck in how to do this. Is there a good video explaining that or if not, could someone please help me get unstuck?

Thank you in advance!

Hi Nev!

Have you looked at this video? It talks about validation as an example, but also covers how to use triggers in general. There are other videos on Cloud Function basics as well.

There are also a couple videos in the Moralis channel on sending email. You’ll need a Sendgrid account for that.

Hi Mayjer,

Thanks for responding so quickly.

Yes I’ve seen the linked video on beforeEvent trigger. I also have configured how to send an email, although only to the current user interacting with the app, not another party. What I’m really stuck on is after the event is created, I know I need to that specific table and then I don’t quite know where to go from there.

I’ll watch the other videos on cloud functions you mentioned as well. Perhaps there are some answers there.

The item which was saved (causing the trigger to fire) can be retrieved from the request object. That should be all the data needed to know (or find out via query) to send the email.

There is all kinds of info inside the request object which is passed to the trigger. I’d recommend using the logger to print the request object to the logs so you can see what it looks like. This will show up in the Dashboard logs. You may need to use JSON.stringify() to get more details.

Moralis.Cloud.afterSave("ItemsSold", async function(request) {
  const logger = Moralis.Cloud.getLogger();
  logger.info(request);
})

Awesome. Thank you very much!!!