30
loading...
This website collects cookies to deliver better user experience
<dependencies>
section of your pom.xml
file.<dependencies>
<dependency>
<groupId>com.mashape.unirest</groupId>
<artifactId>unirest-java</artifactId>
<version>1.4.9</version>
</dependency>
</dependencies>
Ctrl + Shift + O
src/main/java
and name it WelcomeEmail.java
.main
function that fetches all the environment variables and parses the JSON string stored in the environment variable APPWRITE_FUNCTION_EVENT_DATA
. APPWRITE_FUNCTION_EVENT_DATA
is a special environment variable that is automatically passed to your Cloud Function when it is triggered by an event. In our case, the Cloud Function is triggered by a users.create
and account.create
event, so the payload in APPWRITE_FUNCTION_EVENT_DATA
contains a User Object.import com.mashape.unirest.http.exceptions.UnirestException;
import org.json.JSONObject;
public class WelcomeEmail {
private static String YOUR_DOMAIN_NAME;
private static String API_KEY;
public static void main(String[] args) throws UnirestException {
YOUR_DOMAIN_NAME = System.getenv("MAILGUN_DOMAIN");
API_KEY = System.getenv("MAILGUN_API_KEY");
String payload = System.getenv("APPWRITE_FUNCTION_EVENT_DATA");
if (payload != null && !payload.isEmpty()) {
try {
// Parse the JSON string into a JSON Object
JSONObject json = new JSONObject(payload);
String name = json.getString("name");
String email = json.getString("email");
} catch (Exception e) {
System.out.print("[ERROR] There was an error");
System.out.println(e.getMessage());
}
} else {
System.out.println("[INFO] APPWRITE_FUNCTION_EVENT_DATA is empty");
}
}
}
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;
public static String sendSimpleMessage(String name, String email) throws UnirestException {
String message = String.format("Hi %s!", name);
HttpResponse<String> request = Unirest.post("https://api.mailgun.net/v3/" + YOUR_DOMAIN_NAME + "/messages")
.basicAuth("api", API_KEY)
.field("from", "Your Awesome App <[email protected]>")
.field("to", email)
.field("subject", "Welcome Onboard")
.field("text", message)
.asString();
return request.getBody();
}
public static void main(String[] args) throws UnirestException {
YOUR_DOMAIN_NAME = System.getenv("MAILGUN_DOMAIN");
API_KEY = System.getenv("MAILGUN_API_KEY");
String payload = System.getenv("APPWRITE_FUNCTION_EVENT_DATA");
if (payload != null && !payload.isEmpty()) {
try {
JSONObject json = new JSONObject(payload);
String name = json.getString("name");
String email = json.getString("email");
+ String response = sendSimpleMessage(name, email);
+ System.out.println(response);
} catch (Exception e) {
System.out.print("[ERROR] There was an error");
System.out.println(e.getMessage());
}
} else {
System.out.println("[INFO] APPWRITE_FUNCTION_EVENT_DATA is empty");
}
}
.jar
. Fortunately, this can be done really easily using IntelliJ so let's see how..jar
artifacts required to deploy our Cloud Function. Select File > Project Structure > Artifacts
as seen in the screenshot.WelcomeEmail
) as shown in the image.Note: If you don't see the main class, try to follow the steps in this answer https://stackoverflow.com/questions/10654120/error-could-not-find-or-load-main-class-in-intellij-ide
src/main/java/META-INF/MANIFEST.MF
with the following contents.Manifest-Version: 1.0
Main-Class: WelcomeEmail
Build > Build Artifacts > Select your artifact from the list > Build
. You will find the output of this step in out/artifacts/welcome_email_jar/welcome-email.jar
docker run --rm --volume $(pwd):/usr/local/src:rw \
--env MAILGUN_API_KEY="YOUR_API_KEY" \
--env MAILGUN_DOMAIN="sandboxee5d...b85.mailgun.org" \
--env APPWRITE_FUNCTION_EVENT_DATA="{ \"name\" : \"Example\", \"email\": \"[email protected]\"}" \
appwrite/runtime-for-java:11 \
java -jar out/artifacts/welcome_email_jar/welcome-email.jar
{
"id": "<[email protected]>",
"message": "Queued. Thank you."
}
Create
. Keep a note of your function ID as we need this to deploy our function using the CLI.tarfile
.$ cd out/artifacts
$ tar -zcvf code.tar.gz welcome_email_jar
welcome_email_jar/
welcome_email_jar/welcome-email.jar
$ ls
code.tar.gz welcome_email_jar
code.tar.gz
.Appwrite Dashboard > Functions > Overview > Deploy Tag
. In the dialog that pops up, upload the tarfile
we just created and use java -jar welcome-email.jar
for the entry point command. $ cd out/artifacts/
$ appwrite functions createTag --functionId=60d5839682e94 --command="java -jar welcome-email.jar" --code=welcome_email_jar
$id : 60d58cacb0748
functionId : 60d5839682e94
dateCreated : 1624607916
command : java -jar welcome-email.jar
size : 1808608
$ appwrite functions updateTag --functionId=60d5839682e94 --tag=60d58cacb0748
$id : 60d5839682e94
$permissions :
name : Welcome Email Test
dateCreated : 1624605590
dateUpdated : 1624606138
status : disabled
runtime : java-11
tag : 60d58cacb0748
vars :
events :
schedule :
scheduleNext :
schedulePrevious : 0
timeout : 15