페이스북 페이지 만들기
페이스북 개발자 계정 만들기
페이스북 앱 생성
Webhook 서버 제작
@RestController("FacebookWebhookControllerV1")
@RequestMapping("/v1")
public class FacebookWebhookController {
@PostMapping("/facebook/webhook")
public ResponseEntity<Object> webhook(@RequestBody FacebookWebhookRequestDto resource) {
ResponseEntity<Object> response = null;
if (resource.getObject().equals("page")) {
for(Entry entry : resource.getEntry()) {
if(!entry.getMessaging().isEmpty()) {
entry.getMessaging().get(0);
}
}
response = new ResponseEntity<>("EVENT_RECEIVED", HttpStatus.OK);
}
else {
response = new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
return response;
}
@GetMapping("/facebook/webhook")
public ResponseEntity<Object> webhook(@RequestParam(name = "hub.verify_token") String token,
@RequestParam(name = "hub.challenge") String challenge,
@RequestParam(name = "hub.mode") String mode) {
String VERIFY_TOKEN = "new1234!";
ResponseEntity<Object> response = null;
if (mode != null && token != null && token != null) {
// Checks the mode and token sent is correct
if (mode.equals("subscribe") && token.equals(VERIFY_TOKEN)) {
response = new ResponseEntity<>(challenge, HttpStatus.OK);
} else {
// Responds with '403 Forbidden' if verify tokens do not match
response = new ResponseEntity<>(HttpStatus.FORBIDDEN);
}
}
return response;
}
}
페이스북 앱에 메신저 추가 후 Webhook 설정
Spring Boot에 SSL 적용하기