This is an automated email from the ASF dual-hosted git repository.

acosentino pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-examples.git


The following commit(s) were added to refs/heads/main by this push:
     new f9c362f6 ✨ Camel WhatsApp example
f9c362f6 is described below

commit f9c362f6231ab9ebce286ed9775a43b7ba92f6c3
Author: Croway <federico.mariani.1...@gmail.com>
AuthorDate: Fri Jun 10 16:41:30 2022 +0200

    ✨ Camel WhatsApp example
---
 examples/whatsapp/README.adoc                      | 29 +++++++
 examples/whatsapp/pom.xml                          | 94 ++++++++++++++++++++++
 .../apache/camel/example/whatsapp/Application.java | 62 ++++++++++++++
 .../example/whatsapp/WhatsappExamplesRunner.java   | 39 +++++++++
 .../example/whatsapp/WhatsappRouteBuilder.java     | 53 ++++++++++++
 .../src/main/resources/application.properties      | 20 +++++
 6 files changed, 297 insertions(+)

diff --git a/examples/whatsapp/README.adoc b/examples/whatsapp/README.adoc
new file mode 100644
index 00000000..b39454ec
--- /dev/null
+++ b/examples/whatsapp/README.adoc
@@ -0,0 +1,29 @@
+== Whatsapp API Example
+
+This example shows how to use Whatsapp API.
+
+=== How to run
+
+You can run this example using
+
+----
+$ mvn compile exec:java
+----
+
+=== Custom chat and authorization token
+
+You can specify your "phone number id", "authorization token", "recipient 
phone number" and "webhook verify token" in "application.properties" file.
+
+In order to use the webhook and configure it on whatsapp-business interface 
you can use https://ngrok.com/  in order to expose your localhost to the 
internet, by default the webhook is exposed on /camel-whatsapp/webhook
+
+To configure the webhook on whatsapp-business, you can follow this guide 
https://developers.facebook.com/docs/whatsapp/cloud-api/guides/set-up-webhooks.
+
+=== Help and contributions
+
+If you hit any problem using Camel or have some feedback, then please
+https://camel.apache.org/community/support/[let us know].
+
+We also love contributors, so
+https://camel.apache.org/community/contributing/[get involved] :-)
+
+The Camel riders!
diff --git a/examples/whatsapp/pom.xml b/examples/whatsapp/pom.xml
new file mode 100644
index 00000000..a34e5a35
--- /dev/null
+++ b/examples/whatsapp/pom.xml
@@ -0,0 +1,94 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    Licensed to the Apache Software Foundation (ASF) under one or more
+    contributor license agreements.  See the NOTICE file distributed with
+    this work for additional information regarding copyright ownership.
+    The ASF licenses this file to You under the Apache License, Version 2.0
+    (the "License"); you may not use this file except in compliance with
+    the License.  You may obtain a copy of the License at
+
+         http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing, software
+    distributed under the License is distributed on an "AS IS" BASIS,
+    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+    See the License for the specific language governing permissions and
+    limitations under the License.
+
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd";>
+    <modelVersion>4.0.0</modelVersion>
+
+    <parent>
+        <groupId>org.apache.camel.example</groupId>
+        <artifactId>examples</artifactId>
+        <version>3.18.0-SNAPSHOT</version>
+    </parent>
+
+    <artifactId>camel-example-whatsapp</artifactId>
+    <name>Camel :: Example :: Whatsapp</name>
+    <description>An example that uses Whatsapp API</description>
+    <packaging>jar</packaging>
+
+    <properties>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+        <category>Social</category>
+    </properties>
+
+    <dependencyManagement>
+        <dependencies>
+            <!-- Add Camel BOM -->
+            <dependency>
+                <groupId>org.apache.camel</groupId>
+                <artifactId>camel-bom</artifactId>
+                <version>${camel.version}</version>
+                <type>pom</type>
+                <scope>import</scope>
+            </dependency>
+        </dependencies>
+    </dependencyManagement>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-core</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-management</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-whatsapp</artifactId>
+            <version>3.18.0-SNAPSHOT</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-netty-http</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-jsonpath</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>slf4j-simple</artifactId>
+            <version>${slf4j-version}</version>
+        </dependency>
+    </dependencies>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.codehaus.mojo</groupId>
+                <artifactId>exec-maven-plugin</artifactId>
+                <version>${exec-maven-plugin-version}</version>
+                <configuration>
+                    
<mainClass>org.apache.camel.example.whatsapp.Application</mainClass>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+</project>
diff --git 
a/examples/whatsapp/src/main/java/org/apache/camel/example/whatsapp/Application.java
 
b/examples/whatsapp/src/main/java/org/apache/camel/example/whatsapp/Application.java
new file mode 100644
index 00000000..bdabb687
--- /dev/null
+++ 
b/examples/whatsapp/src/main/java/org/apache/camel/example/whatsapp/Application.java
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.example.whatsapp;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Properties;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.impl.DefaultCamelContext;
+
+public final class Application {
+
+    public static final String AUTHORIZATION_TOKEN;
+    public static final String PHONE_NUMBER_ID;
+    public static final String RECIPIENT_PHONE_NUMBER;
+    public static final String WEBHOOK_VERIFY_TOKEN;
+
+    static {
+        Properties properties = new Properties();
+        ClassLoader loader = Application.class.getClassLoader();
+        try (InputStream resourceStream = 
loader.getResourceAsStream("application.properties")) {
+            properties.load(resourceStream);
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+
+        AUTHORIZATION_TOKEN = properties.getProperty("authorizationToken");
+        PHONE_NUMBER_ID = properties.getProperty("phoneNumberId");
+        RECIPIENT_PHONE_NUMBER = 
properties.getProperty("recipientPhoneNumber");
+        WEBHOOK_VERIFY_TOKEN = properties.getProperty("webhookVerifyToken");
+    }
+
+    private Application() {
+        // noop
+    }
+
+    public static void main(String[] args) throws Exception {
+        try (CamelContext context = new DefaultCamelContext()) {
+            context.start();
+
+            context.addRoutes(new WhatsappRouteBuilder());
+            context.addStartupListener(new WhatsappExamplesRunner());
+            
+            Thread.sleep(6000000);
+        }
+    }
+}
diff --git 
a/examples/whatsapp/src/main/java/org/apache/camel/example/whatsapp/WhatsappExamplesRunner.java
 
b/examples/whatsapp/src/main/java/org/apache/camel/example/whatsapp/WhatsappExamplesRunner.java
new file mode 100644
index 00000000..b9869fca
--- /dev/null
+++ 
b/examples/whatsapp/src/main/java/org/apache/camel/example/whatsapp/WhatsappExamplesRunner.java
@@ -0,0 +1,39 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.example.whatsapp;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.StartupListener;
+import org.apache.camel.component.whatsapp.model.TextMessage;
+import org.apache.camel.component.whatsapp.model.TextMessageRequest;
+
+public class WhatsappExamplesRunner implements StartupListener {
+    
+    private static final String CAMEL_EMOJI = "\uD83D\uDC2B";
+
+    @Override
+    public void onCamelContextStarted(CamelContext context, boolean 
alreadyStarted) throws Exception {
+        // Send message to whatsapp
+        TextMessageRequest request = new TextMessageRequest();
+        request.setTo(Application.RECIPIENT_PHONE_NUMBER);
+        request.setText(new TextMessage());
+        request.getText().setBody("This is an auto-generated message from 
Camel " + CAMEL_EMOJI);
+        
+        context.createProducerTemplate().requestBody("direct:start", request);
+    }
+}
+
diff --git 
a/examples/whatsapp/src/main/java/org/apache/camel/example/whatsapp/WhatsappRouteBuilder.java
 
b/examples/whatsapp/src/main/java/org/apache/camel/example/whatsapp/WhatsappRouteBuilder.java
new file mode 100644
index 00000000..2d700e34
--- /dev/null
+++ 
b/examples/whatsapp/src/main/java/org/apache/camel/example/whatsapp/WhatsappRouteBuilder.java
@@ -0,0 +1,53 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.example.whatsapp;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.whatsapp.model.TextMessage;
+import org.apache.camel.component.whatsapp.model.TextMessageRequest;
+
+public class WhatsappRouteBuilder extends RouteBuilder {
+
+    @Override
+    public void configure() {
+        restConfiguration().port(8080);
+
+        // Send custom message
+        from("direct:start")
+            .toF("whatsapp:%s/?authorizationToken=%s", 
Application.PHONE_NUMBER_ID, Application.AUTHORIZATION_TOKEN);
+
+        // Echo to a message sent from the number
+        
fromF("webhook:whatsapp:%s?authorizationToken=%s&webhookVerifyToken=%s", 
Application.PHONE_NUMBER_ID, Application.AUTHORIZATION_TOKEN, 
Application.WEBHOOK_VERIFY_TOKEN)
+            .log("${body}")
+            .choice().when().jsonpath("$.entry[0].changes[0].value.messages", 
true)
+            
.setHeader("CamelWhatsappBody").jsonpath("$.entry[0].changes[0].value.messages[0].text.body")
+            
.setHeader("CamelWhatsappSentMessage").jsonpath("$.entry[0].changes[0].value.contacts[0].profile.name")
+            
.setHeader("CamelWhatsappPhoneNumber").jsonpath("$.entry[0].changes[0].value.contacts[0].wa_id")
+            .process(exchange -> {
+                TextMessageRequest request = new TextMessageRequest();
+                
request.setTo(exchange.getIn().getHeader("CamelWhatsappPhoneNumber").toString());
+                request.setText(new TextMessage());
+                request.getText().setBody(String.format("Hello %s message sent 
was %s", 
+                                                        
exchange.getIn().getHeader("CamelWhatsappSentMessage"),
+                                                        
exchange.getIn().getHeader("CamelWhatsappBody")));
+                
+                exchange.getIn().setBody(request);
+            })
+            .toF("whatsapp:%s/?authorizationToken=%s", 
Application.PHONE_NUMBER_ID, Application.AUTHORIZATION_TOKEN)
+        .end();
+    }
+}
diff --git a/examples/whatsapp/src/main/resources/application.properties 
b/examples/whatsapp/src/main/resources/application.properties
new file mode 100644
index 00000000..75d2e572
--- /dev/null
+++ b/examples/whatsapp/src/main/resources/application.properties
@@ -0,0 +1,20 @@
+## ---------------------------------------------------------------------------
+## Licensed to the Apache Software Foundation (ASF) under one or more
+## contributor license agreements.  See the NOTICE file distributed with
+## this work for additional information regarding copyright ownership.
+## The ASF licenses this file to You under the Apache License, Version 2.0
+## (the "License"); you may not use this file except in compliance with
+## the License.  You may obtain a copy of the License at
+##
+##      http://www.apache.org/licenses/LICENSE-2.0
+##
+## Unless required by applicable law or agreed to in writing, software
+## distributed under the License is distributed on an "AS IS" BASIS,
+## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+## See the License for the specific language governing permissions and
+## limitations under the License.
+## ---------------------------------------------------------------------------
+authorizationToken=
+phoneNumberId=
+recipientPhoneNumber=
+webhookVerifyToken=
\ No newline at end of file

Reply via email to