BotMill / fb-botmill

A Java framework for building bots on Facebook's Messenger Platform.
MIT License
72 stars 26 forks source link

How to manage Facebook "FACEBOOK_WELCOME" #56

Closed fritzZz closed 7 years ago

fritzZz commented 7 years ago

I'm trying to automatically reply to the first event sent by facebook :

JSON input: {"object":"page","entry":[{"id":"683477205158863","time":1485793125243,"messaging":[{"recipient":{"id":"683477205158863"},"timestamp":1485793125243,"sender":{"id":"1108634062596634"},"postback":{"payload":"FACEBOOK_WELCOME"}}]}]}

But I've not found code snippet to manage this case. Do you have suggestions?

aurasphere commented 7 years ago

Hi fritzZz!

Thank you for choosing BotMill!

If you are trying to match any event from Facebook you can use the AnyEvent class. If you want to match any postback event, you can use AnyPostbackEvent. If you want to match a specific postback you can use PostbackEvent.

Here's an example on how to use each of them:

addActionFrame(new AnyEvent(), new MessageAutoReply("Any event received!")); addActionFrame(new AnyPostbackEvent(), new MessageAutoReply("Any postback event received!")); addActionFrame(new PostbackEvent("FACEBOOK_WELCOME"), new MessageAutoReply("FACEBOOK_WELCOME received!"));

Please let me know if this doesn't provide an answer to your question!

Thank you!

fritzZz commented 7 years ago

Thank you Aurasphere!

I tried your snippets. I receive the event and seems to manage well the ActionFrame but no message has been received from the bot. I put here some log maybe you could help me:

2017-01-31T11:30:13.650093+00:00 app[web.1]: 11:30:13.649 [http-nio-49206-exec-6] DEBUG c.a.botmill.fb.FbBotMillServlet - JSON input: {"object":"page","entry":[{"id":"683477205158863","time":1485862213121,"messaging":[{"recipient":{"id":"683477205158863"},"timestamp":1485862213121,"sender":{"id":"1108634062596634"},"postback":{"payload":"FACEBOOK_WELCOME"}}]}]} 2017-01-31T11:30:13.656156+00:00 app[web.1]: 11:30:13.655 [http-nio-49206-exec-6] DEBUG o.h.v.i.e.r.DefaultTraversableResolver - Cannot find javax.persistence.Persistence on classpath. Assuming non JPA 2 environment. All properties will per default be traversable. 2017-01-31T11:30:13.656431+00:00 app[web.1]: 11:30:13.656 [http-nio-49206-exec-6] DEBUG o.h.v.i.xml.ValidationXmlParser - Trying to load META-INF/validation.xml for XML based Validator configuration. 2017-01-31T11:30:13.656543+00:00 app[web.1]: 11:30:13.656 [http-nio-49206-exec-6] DEBUG o.h.v.i.xml.ResourceLoaderHelper - Trying to load META-INF/validation.xml via TCCL 2017-01-31T11:30:13.657198+00:00 app[web.1]: 11:30:13.657 [http-nio-49206-exec-6] DEBUG o.h.v.i.xml.ResourceLoaderHelper - Trying to load META-INF/validation.xml via Hibernate Validator's class loader 2017-01-31T11:30:13.657551+00:00 app[web.1]: 11:30:13.657 [http-nio-49206-exec-6] DEBUG o.h.v.i.xml.ValidationXmlParser - No META-INF/validation.xml found. Using annotation based configuration only. 2017-01-31T11:30:13.733788+00:00 app[web.1]: 11:30:13.733 [http-nio-49206-exec-6] DEBUG o.h.v.i.m.d.ConstraintDescriptorImpl - Adding composing constraint: ConstraintDescriptorImpl{annotation=javax.validation.constraints.NotNull, payloads=[], hasComposingConstraints=true, isReportAsSingleInvalidConstraint=false, elementType=FIELD, definedOn=DEFINED_LOCALLY, groups=[interface javax.validation.groups.Default], attributes={message={javax.validation.constraints.NotNull.message}, payload=[Ljava.lang.Class;@52e980d5, groups=[Ljava.lang.Class;@16b4ca4e}, constraintType=GENERIC}. 2017-01-31T11:30:13.755120+00:00 app[web.1]: 11:30:13.754 [http-nio-49206-exec-6] DEBUG o.h.v.i.m.d.ConstraintDescriptorImpl - Adding composing constraint: ConstraintDescriptorImpl{annotation=javax.validation.constraints.NotNull, payloads=[], hasComposingConstraints=true, isReportAsSingleInvalidConstraint=false, elementType=FIELD, definedOn=DEFINED_LOCALLY, groups=[interface javax.validation.groups.Default], attributes={message={javax.validation.constraints.NotNull.message}, payload=[Ljava.lang.Class;@488d3d5f, groups=[Ljava.lang.Class;@5b70f607}, constraintType=GENERIC}. 2017-01-31T11:30:13.774979+00:00 app[web.1]: 11:30:13.774 [http-nio-49206-exec-6] DEBUG c.a.b.f.i.util.network.NetworkUtils - Request: {"message":{"text":"FACEBOOK_WELCOME received!"},"recipient":{"id":"1108634062596634"}} 2017-01-31T11:30:13.803264+00:00 app[web.1]: 11:30:13.803 [http-nio-49206-exec-6] DEBUG c.a.b.f.i.util.network.NetworkUtils - POST https://graph.facebook.com/v2.7/me/messages?access_token=EAAPGisJ3ISQBANBm70rrOEuyG3efkyYuCBO1wL4y1eWLZCL9pcY9N1nrgyxOCCuTGMsV1RNmD0ygkGmZBpc0ArNCqyJvSeAj4Ivrgm6KrGdOwx5YdnSJ5xMtZCttPGkbVACbCAIiGW1aiLDWSTFU1V5N3RGk8wZAto1TsmdTewZDZD HTTP/1.1 2017-01-31T11:30:14.085841+00:00 heroku[router]: at=info method=POST path="/myfirstbotservlet" host=polar-reef-13963.herokuapp.com request_id=c5db45e0-1b92-43be-9274-7472bb6c8efb fwd="31.13.114.71" dyno=web.1 connect=0ms service=439ms status=200 bytes=121 2017-01-31T11:30:14.224086+00:00 heroku[router]: at=info method=POST path="/myfirstbotservlet" host=polar-reef-13963.herokuapp.com request_id=94ffe883-7a78-4338-baa7-7a923939c718 fwd="173.252.88.87" dyno=web.1 connect=0ms service=3ms status=200 bytes=121 2017-01-31T11:30:14.080678+00:00 app[web.1]: 11:30:14.079 [http-nio-49206-exec-6] DEBUG c.a.b.f.i.util.network.NetworkUtils - HTTP Status Code: 200 2017-01-31T11:30:14.081565+00:00 app[web.1]: 11:30:14.080 [http-nio-49206-exec-6] DEBUG c.a.b.f.i.util.network.NetworkUtils - Confirmation from Facebook. Recipient ID: [1108634062596634], Message ID: [mid.1485862214026:607fee8855], Result Message: [null] 2017-01-31T11:30:14.222951+00:00 app[web.1]: 11:30:14.222 [http-nio-49206-exec-7] DEBUG c.a.botmill.fb.FbBotMillServlet - JSON input: {"object":"page","entry":[{"id":"683477205158863","time":1485862214041,"messaging":[{"sender":{"id":"683477205158863"},"recipient":{"id":"1108634062596634"},"timestamp":1485862214026,"message":{"is_echo":true,"app_id":1062724200505636,"mid":"mid.1485862214026:607fee8855","seq":34344,"text":"FACEBOOK_WELCOME received!"}}]}]} 2017-01-31T11:30:14.770540+00:00 app[web.1]: 11:30:14.770 [http-nio-49206-exec-8] DEBUG c.a.botmill.fb.FbBotMillServlet - JSON input: {"object":"page","entry":[{"id":"683477205158863","time":1485862214642,"messaging":[{"sender":{"id":"1108634062596634"},"recipient":{"id":"683477205158863"},"timestamp":1485862214641,"delivery":{"mids":["mid.1485862214026:607fee8855"],"watermark":1485862214026,"seq":0}}]}]} 2017-01-31T11:30:14.771027+00:00 heroku[router]: at=info method=POST path="/myfirstbotservlet" host=polar-reef-13963.herokuapp.com request_id=4afbb3f8-2d31-4507-8231-c912516b1617 fwd="173.252.90.233" dyno=web.1 connect=1ms service=3ms status=200 bytes=121

Another question... If I would like to manage the events, and the incoming messages for example integrating Api.Ai how could I process the ActionFrame with a custom business logic? Is there any way to add custom behaviours ?

aurasphere commented 7 years ago

I tried out the code myself just now and it works for me. Could you share your bot behavior?

About your second question, the answer is yes. The way you do this depends on what you really want to do. The ActionFrame class provides 2 hooks that you can override (names are self explanatory): beforeReply and afterReply. Notice that these methods doesn't return anything since they are only intended for reading and eventually storing the message.

If you want to manipulate the reply according to the incoming message though, you should really use your own implementation of AutoReply. If you need to have different AutoReplies share the same common behavior, you could implement an abstract AutoReply which does the common behavior in the createResponse method and then delegates an abstract method of yours to provide the actual reply. Here's an example of this:

public  abstract class CustomBehaviorAutoReply extends AutoReply {

  @Override
   public FbBotMillResponse createResponse(MessageEnvelope envelope){
       // Your logic here
       return myAbstractMethod(envelope);
   }

   public abstract FbBotMillResponse myAbstractMethod(MessageEnvelope envelope);

}
fritzZz commented 7 years ago

I solved the problem in the night. Thank you very much for your answers! Now everythink it's clear ;)