whatsapp-business-java-api
whatsapp-business-java-api copied to clipboard
WebHook Config
Please tell me how to use Webhook, the example class is not helpful for how it should work. Thanks in advance, any help would be greatly appreciated 🙏
Hello and welcome! We're glad to see that you've opened your first issue. We appreciate your contribution and would love to hear more about the problem you're experiencing. Our team is actively monitoring this repository and we will do our best to respond to your issue as soon as possible. Thank you for using our project and we look forward to working with you!
Hello @sradityaa, if you're using Spring Boot, you should implement a controller to receive notifications sent by WhatsApp servers. I don't have a code example, but you can take a look at this link
You can use this SDK to deserialize an event. Here's an example.
I hope this helps. When I have more examples, I'll post them here.
hey @sradityaa , check the SpringBoot example below.
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/whatsapp")
@SpringBootApplication
public class WhatsappWebhook {
private static final Logger log = LoggerFactory.getLogger(WhatsappWebhook.class);
public static void main(String[] args) {
SpringApplication.run(WhatsappWebhook.class, args);
}
@GetMapping(path = "/webhook")
public String get(@RequestParam(value = "hub.challenge") String challenge,
@RequestParam(value = "hub.verify_token") String verifyToken) {
if (log.isDebugEnabled()) {
log.debug("Challenge received: {}", challenge);
}
if ("myWebhookToken".equals(verifyToken)) {
log.info("Facebook Verify Token checked succesfully");
return challenge;
} else {
log.info("Invalid Facebook Verify Token");
return "error";
}
}
@PostMapping(path = "/webhook", consumes = "application/json")
public void post(@RequestBody String payload) {
log.debug("Event received: \n {}", payload);
}
}
Don't forget to setup your webhook with the correct token (myWebhookToken
as in the example) and the url https://mydomain.com/whatsapp/webhook
I hope this helps