Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can I send WhatsApp Business Platform Cloud API message using restfb #1225

Closed
astrit-hakobyan opened this issue Jun 2, 2022 · 6 comments
Closed
Assignees
Milestone

Comments

@astrit-hakobyan
Copy link

astrit-hakobyan commented Jun 2, 2022

I see WABP integration is in upcoming todo. Currently can I send simple whatsapp Media Messages using restfb?

curl -X  POST \
 'https://graph.facebook.com/v13.0/FROM_PHONE_NUMBER_ID/messages' \
 -H 'Authorization: Bearer ACCESS_TOKEN' \
 -d '{
  "messaging_product": "whatsapp",
  "recipient_type": "individual",
  "to": "PHONE_NUMBER",
  "type": "image",
  "image": {
    "id" : "MEDIA_OBJECT_ID"
  }
}'
@nbartels
Copy link
Contributor

nbartels commented Jun 2, 2022

As I see, the problem is the data. The publish methods we have so far, can only post form data. But we have no way so far to post a "simple" json. I think this should be one of the next steps before supporting all the objects.

The access token in the header is not a problem and the DefaultFacebookClient has already the setHeaderAuthorization method in place.

@nbartels nbartels added this to the 2022.7.0 milestone Jun 2, 2022
@nbartels nbartels self-assigned this Jun 2, 2022
@nbartels
Copy link
Contributor

nbartels commented Jun 3, 2022

As short update. I will use this issue to add a way to post a JSON as "body" to an endpoint as alternative to send the data in a form.

This should solve the basic problem, and you can start (as soon as the release is published) using RestFB to send WhatsApp messages.

The concept is ready so far, and I already started the implementation, but the unit tests are missing atm. Hopefully everything's ready after the weekend and this great feature will be available next week.

Thanks for the input/question 😀

@nbartels
Copy link
Contributor

nbartels commented Jun 5, 2022

With the next release it will be possible using a code similar to this snippet:

DefaultFacebookClient fbClient = new DefaultFacebookClient("<access token>", Version.VERSION_13_0);
fbClient.setHeaderAuthorization(true);
JsonObject message = new JsonObject();
// fill message JSON here - as long as we have no WABP objects in place 
Body body = Body.withData(message);
JsonObject result = fbClient.publish("FROM_PHONE_NUMBER_ID/messages", JsonObject.class, body);

@nbartels
Copy link
Contributor

nbartels commented Jun 5, 2022

I have to correct the code.
After writing an integration test, this is the code that sends an WA message.

DefaultFacebookClient fbClient = new DefaultFacebookClient(waAccessToken, Version.VERSION_13_0);
fbClient.setHeaderAuthorization(true);
    
JsonObject textJso = new JsonObject();
textJso.add("preview_url", false);
textJso.add("body", "this is a short test message");

Parameter[] params = new Parameter[] { //
        Parameter.with("messaging_product", "whatsapp"), //
        Parameter.with("recipient_type", "individual"), //
        Parameter.with("type", "text"), //
        Parameter.with("to", "<recipient number>"), //
        Parameter.with("text", textJso)};

// Send the data and get the result as JsonObject
JsonObject jso = fbClient.publish(sendWaId + "/messages", JsonObject.class, params);
System.out.println(jso.toString());

nbartels added a commit that referenced this issue Jun 6, 2022
@nbartels
Copy link
Contributor

nbartels commented Jun 6, 2022

With the latest fix on this issue, it is possible to post a json object direct as body.
So you have the choice to use parameters or the body.

Using body looks like:

DefaultFacebookClient fbClient = new DefaultFacebookClient(waAccessToken, Version.VERSION_13_0);
fbClient.setHeaderAuthorization(true);

JsonObject sJso = new JsonObject();
sJso.add("messaging_product", "whatsapp");
sJso.add("recipient_type", "individual");
sJso.add("type", "text");
sJso.add("to", "<recipient number>");

JsonObject textJso = new JsonObject();
textJso.add("preview_url", false);
textJso.add("body", "this is a short test message");
sJso.add("text", textJso);

// post json as body
JsonObject jso = fbClient.publish(sendWaId + "/messages", JsonObject.class, Body.withData(sJso));
System.out.println(jso.toString());

@astrit-hakobyan
Copy link
Author

Thank you for fast response

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 6, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants