├── .classpath ├── .gitignore ├── .project ├── .travis.yml ├── HOWTO.md ├── LICENSE ├── README.md ├── pom.xml ├── screenshots ├── msg_postback.png └── msg_text.png └── src └── main ├── java └── in │ └── strollup │ └── fb │ ├── contract │ ├── Attachment.java │ ├── Button.java │ ├── Delivery.java │ ├── Element.java │ ├── Entry.java │ ├── FbMsgRequest.java │ ├── Message.java │ ├── Messaging.java │ ├── Payload.java │ ├── Postback.java │ ├── Recipient.java │ └── Sender.java │ ├── profile │ └── FbProfile.java │ ├── servlet │ └── WebHookServlet.java │ └── utils │ └── FbChatHelper.java └── webapp ├── WEB-INF └── web.xml └── index.jsp /.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | /.settings/ 3 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Java-FbChatBot 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.wst.jsdt.core.javascriptValidator 10 | 11 | 12 | 13 | 14 | org.eclipse.jdt.core.javabuilder 15 | 16 | 17 | 18 | 19 | org.eclipse.wst.common.project.facet.core.builder 20 | 21 | 22 | 23 | 24 | org.eclipse.wst.validation.validationbuilder 25 | 26 | 27 | 28 | 29 | org.eclipse.m2e.core.maven2Builder 30 | 31 | 32 | 33 | 34 | 35 | org.eclipse.jem.workbench.JavaEMFNature 36 | org.eclipse.wst.common.modulecore.ModuleCoreNature 37 | org.eclipse.jdt.core.javanature 38 | org.eclipse.m2e.core.maven2Nature 39 | org.eclipse.wst.common.project.facet.core.nature 40 | org.eclipse.wst.jsdt.core.jsNature 41 | 42 | 43 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | jdk: 3 | - oraclejdk8 4 | script: mvn install -DskipTests=true -Dmaven.javadoc.skip=true -B -V 5 | notifications: 6 | email: siddharth.dce08@gmail.com 7 | 8 | -------------------------------------------------------------------------------- /HOWTO.md: -------------------------------------------------------------------------------- 1 | ## How to deploy locally 2 | - checkout and export this project as maven project in eclipse 3 | - create a run configuration for this project 4 | - with goals: `jetty:run -Djetty.port=9090` 5 | - with name: `Java-FbChatBot` 6 | - with base directory: `${workspace_loc:/Java-FbChatBot}` 7 | - you can change the port (9090) to whatever you want 8 | - check your project 9 | - [home](http://localhost:9090/bot/) 10 | - [webhook](http://localhost:9090/bot/webhook?hub.verify_token=whatever_string_you_or_your_friends_wish&hub.challenge=your_name) 11 | 12 | 13 | ## run on ngrok 14 | - now you have to setup [ngrok](https://ngrok.com/) which is the easiest way to expose your local server to the world 15 | - now run ngrok for port 9090 as `ngrok http -subdomain=mybot -log=stdout 9090` 16 | - check your project 17 | - [home](https://mybot.ngrok.io/bot/) 18 | - [webhook](https://mybot.ngrok.io/bot/webhook?hub.verify_token=whatever_string_you_or_your_friends_wish&hub.challenge=your_name) 19 | - now add webhook url as show in [fb tutorial](https://developers.facebook.com/docs/messenger-platform/implementation#setup_webhook) 20 | 21 | ## output in fb messenger 22 | - once you are done with fb webhook setup, you can message on your page and will get automated replies as shown in pictures below 23 | 24 | Normal text by a user 25 | 26 | ![Normal text](screenshots/msg_text.png) 27 | 28 | Postbacks (button click by a user in messenger) 29 | 30 | ![PostBacks](screenshots/msg_postback.png) 31 | 32 | 33 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Siddharth Kumar (https://www.strollup.in) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Java-FbChatBot [![Build Status](https://api.travis-ci.org/thekosmix/Java-FbChatBot.png)](http://travis-ci.org/thekosmix/Java-FbChatBot) 2 | This is an implementation of Fb chat bot in java. It shows how to create Fb messenger webhook in java, contracts and PoJos of webhook. This project can be used to setup fb messenger webhook with existing tomcat/jetty/java project. 3 | 4 | ## How to setup 5 | - read [HOWTO.md](HOWTO.md) 6 | 7 | ## Screenshots 8 | ### Normal text by a user 9 | 10 | ![Normal text](screenshots/msg_text.png) 11 | 12 | ### Postbacks (button click by a user in messenger) 13 | 14 | ![PostBacks](screenshots/msg_postback.png) 15 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | in.strollup.fb 5 | bot 6 | war 7 | 0.0.1-SNAPSHOT 8 | Java-FbChatBot 9 | http://maven.apache.org 10 | 11 | 12 | org.apache.httpcomponents 13 | httpclient 14 | 4.5.13 15 | 16 | 17 | javax.servlet 18 | servlet-api 19 | 2.5 20 | 21 | 22 | com.google.code.gson 23 | gson 24 | 2.8.9 25 | 26 | 27 | 28 | org.apache.commons 29 | commons-lang3 30 | 3.4 31 | 32 | 33 | 34 | 35 | 36 | 37 | org.mortbay.jetty 38 | maven-jetty-plugin 39 | 6.1.10 40 | 41 | 10 42 | 43 | 44 | 45 | org.apache.maven.plugins 46 | maven-compiler-plugin 47 | 2.3.2 48 | 49 | 1.7 50 | 1.7 51 | UTF-8 52 | 53 | 54 | 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /screenshots/msg_postback.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thekosmix/Java-FbChatBot/5867c1f9bf25f5e6c4759411ae66b3a28033e10d/screenshots/msg_postback.png -------------------------------------------------------------------------------- /screenshots/msg_text.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thekosmix/Java-FbChatBot/5867c1f9bf25f5e6c4759411ae66b3a28033e10d/screenshots/msg_text.png -------------------------------------------------------------------------------- /src/main/java/in/strollup/fb/contract/Attachment.java: -------------------------------------------------------------------------------- 1 | 2 | package in.strollup.fb.contract; 3 | 4 | import javax.annotation.Generated; 5 | import com.google.gson.annotations.Expose; 6 | import com.google.gson.annotations.SerializedName; 7 | import org.apache.commons.lang3.builder.EqualsBuilder; 8 | import org.apache.commons.lang3.builder.HashCodeBuilder; 9 | import org.apache.commons.lang3.builder.ToStringBuilder; 10 | 11 | @Generated("org.jsonschema2pojo") 12 | public class Attachment { 13 | 14 | @SerializedName("type") 15 | @Expose 16 | private String type; 17 | @SerializedName("payload") 18 | @Expose 19 | private Payload payload; 20 | 21 | /** 22 | * 23 | * @return 24 | * The type 25 | */ 26 | public String getType() { 27 | return type; 28 | } 29 | 30 | /** 31 | * 32 | * @param type 33 | * The type 34 | */ 35 | public void setType(String type) { 36 | this.type = type; 37 | } 38 | 39 | /** 40 | * 41 | * @return 42 | * The payload 43 | */ 44 | public Payload getPayload() { 45 | return payload; 46 | } 47 | 48 | /** 49 | * 50 | * @param payload 51 | * The payload 52 | */ 53 | public void setPayload(Payload payload) { 54 | this.payload = payload; 55 | } 56 | 57 | @Override 58 | public String toString() { 59 | return ToStringBuilder.reflectionToString(this); 60 | } 61 | 62 | @Override 63 | public int hashCode() { 64 | return new HashCodeBuilder().append(type).append(payload).toHashCode(); 65 | } 66 | 67 | @Override 68 | public boolean equals(Object other) { 69 | if (other == this) { 70 | return true; 71 | } 72 | if ((other instanceof Attachment) == false) { 73 | return false; 74 | } 75 | Attachment rhs = ((Attachment) other); 76 | return new EqualsBuilder().append(type, rhs.type).append(payload, rhs.payload).isEquals(); 77 | } 78 | 79 | } 80 | -------------------------------------------------------------------------------- /src/main/java/in/strollup/fb/contract/Button.java: -------------------------------------------------------------------------------- 1 | package in.strollup.fb.contract; 2 | 3 | import javax.annotation.Generated; 4 | import org.apache.commons.lang3.builder.EqualsBuilder; 5 | import org.apache.commons.lang3.builder.HashCodeBuilder; 6 | import org.apache.commons.lang3.builder.ToStringBuilder; 7 | import com.google.gson.annotations.Expose; 8 | import com.google.gson.annotations.SerializedName; 9 | 10 | @Generated("org.jsonschema2pojo") 11 | public class Button { 12 | 13 | @SerializedName("type") 14 | @Expose 15 | private String type; 16 | @SerializedName("title") 17 | @Expose 18 | private String title; 19 | @SerializedName("payload") 20 | @Expose 21 | private String payload; 22 | @SerializedName("url") 23 | @Expose 24 | private String url; 25 | 26 | public String getUrl() { 27 | return url; 28 | } 29 | 30 | public void setUrl(String url) { 31 | this.url = url; 32 | } 33 | 34 | /** 35 | * 36 | * @return The type 37 | */ 38 | public String getType() { 39 | return type; 40 | } 41 | 42 | /** 43 | * 44 | * @param type 45 | * The type 46 | */ 47 | public void setType(String type) { 48 | this.type = type; 49 | } 50 | 51 | /** 52 | * 53 | * @return The title 54 | */ 55 | public String getTitle() { 56 | return title; 57 | } 58 | 59 | /** 60 | * 61 | * @param title 62 | * The title 63 | */ 64 | public void setTitle(String title) { 65 | this.title = title; 66 | } 67 | 68 | /** 69 | * 70 | * @return The payload 71 | */ 72 | public String getPayload() { 73 | return payload; 74 | } 75 | 76 | /** 77 | * 78 | * @param payload 79 | * The payload 80 | */ 81 | public void setPayload(String payload) { 82 | this.payload = payload; 83 | } 84 | 85 | @Override 86 | public String toString() { 87 | return ToStringBuilder.reflectionToString(this); 88 | } 89 | 90 | @Override 91 | public int hashCode() { 92 | return new HashCodeBuilder().append(type).append(title).append(payload).toHashCode(); 93 | } 94 | 95 | @Override 96 | public boolean equals(Object other) { 97 | if (other == this) { 98 | return true; 99 | } 100 | if ((other instanceof Button) == false) { 101 | return false; 102 | } 103 | Button rhs = ((Button) other); 104 | return new EqualsBuilder().append(type, rhs.type).append(title, rhs.title).append(payload, rhs.payload) 105 | .isEquals(); 106 | } 107 | 108 | } 109 | -------------------------------------------------------------------------------- /src/main/java/in/strollup/fb/contract/Delivery.java: -------------------------------------------------------------------------------- 1 | package in.strollup.fb.contract; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | import javax.annotation.Generated; 6 | import org.apache.commons.lang3.builder.EqualsBuilder; 7 | import org.apache.commons.lang3.builder.HashCodeBuilder; 8 | import org.apache.commons.lang3.builder.ToStringBuilder; 9 | import com.google.gson.annotations.Expose; 10 | import com.google.gson.annotations.SerializedName; 11 | 12 | @Generated("org.jsonschema2pojo") 13 | public class Delivery { 14 | 15 | @SerializedName("mids") 16 | @Expose 17 | private List mids = new ArrayList(); 18 | @SerializedName("watermark") 19 | @Expose 20 | private Long watermark; 21 | @SerializedName("seq") 22 | @Expose 23 | private Long seq; 24 | 25 | /** 26 | * 27 | * @return The mids 28 | */ 29 | public List getMids() { 30 | return mids; 31 | } 32 | 33 | /** 34 | * 35 | * @param mids 36 | * The mids 37 | */ 38 | public void setMids(List mids) { 39 | this.mids = mids; 40 | } 41 | 42 | /** 43 | * 44 | * @return The watermark 45 | */ 46 | public Long getWatermark() { 47 | return watermark; 48 | } 49 | 50 | /** 51 | * 52 | * @param watermark 53 | * The watermark 54 | */ 55 | public void setWatermark(Long watermark) { 56 | this.watermark = watermark; 57 | } 58 | 59 | /** 60 | * 61 | * @return The seq 62 | */ 63 | public Long getSeq() { 64 | return seq; 65 | } 66 | 67 | /** 68 | * 69 | * @param seq 70 | * The seq 71 | */ 72 | public void setSeq(Long seq) { 73 | this.seq = seq; 74 | } 75 | 76 | @Override 77 | public String toString() { 78 | return ToStringBuilder.reflectionToString(this); 79 | } 80 | 81 | @Override 82 | public int hashCode() { 83 | return new HashCodeBuilder().append(mids).append(watermark).append(seq).toHashCode(); 84 | } 85 | 86 | @Override 87 | public boolean equals(Object other) { 88 | if (other == this) { 89 | return true; 90 | } 91 | if ((other instanceof Delivery) == false) { 92 | return false; 93 | } 94 | Delivery rhs = ((Delivery) other); 95 | return new EqualsBuilder().append(mids, rhs.mids).append(watermark, rhs.watermark).append(seq, rhs.seq) 96 | .isEquals(); 97 | } 98 | 99 | } -------------------------------------------------------------------------------- /src/main/java/in/strollup/fb/contract/Element.java: -------------------------------------------------------------------------------- 1 | package in.strollup.fb.contract; 2 | 3 | import java.util.List; 4 | import javax.annotation.Generated; 5 | import org.apache.commons.lang3.builder.EqualsBuilder; 6 | import org.apache.commons.lang3.builder.HashCodeBuilder; 7 | import org.apache.commons.lang3.builder.ToStringBuilder; 8 | import com.google.gson.annotations.Expose; 9 | import com.google.gson.annotations.SerializedName; 10 | 11 | @Generated("org.jsonschema2pojo") 12 | public class Element { 13 | 14 | @SerializedName("title") 15 | @Expose 16 | private String title; 17 | @SerializedName("subtitle") 18 | @Expose 19 | private String subtitle; 20 | @SerializedName("image_url") 21 | @Expose 22 | private String imageUrl; 23 | @SerializedName("buttons") 24 | @Expose 25 | private List