1. API 개발
1-1. Model
1) Keyboard
package com.kakaobot.springboot.model;
import java.util.List;
public class KakaoKeyboard {
// Button : 객관식 응답의 목록을 구성할 수 있음
// text : 주관식 응답을 입력받을 수 있음
private String type;
// 객관식 응답 내용의 목록
private List<String> buttons;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public List<String> getButtons() {
return buttons;
}
public void setButtons(List<String> buttons) {
this.buttons = buttons;
}
@Override
public String toString() {
return "KakaoKeyboard{" +
"type='" + type + '\'' +
", buttons=" + buttons +
'}';
}
}
2) Message
public class KakaoPhoto {
// 이미지 url
private String url;
// 이미지 width
private int width;
// 이미지 height
private int height;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
@Override
public String toString() {
return "KakaoPhoto{" +
"url='" + url + '\'' +
", width=" + width +
", height=" + height +
'}';
}
}
public class KakaoMessageButton {
// 링크버튼의 타이틀
private String label;
// 링크버튼의 연결 링크 주소
private String url;
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
@Override
public String toString() {
return "KakaoMessageButton{" +
"label='" + label + '\'' +
", url='" + url + '\'' +
'}';
}
}
public class KakaoMessage implements Serializable {
// 사용자에게 발송될 메시지 텍스트 (최대 1000자)
private String text;
// 말풍선에 들어갈 이미지 정보
private KakaoPhoto photo;
// 말풍선에 붙는 링크버튼 정보
@JsonProperty("message_button")
private KakaoMessageButton messageButton;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public KakaoPhoto getPhoto() {
return photo;
}
public void setPhoto(KakaoPhoto photo) {
this.photo = photo;
}
public KakaoMessageButton getMessageButton() {
return messageButton;
}
public void setMessageButton(KakaoMessageButton messageButton) {
this.messageButton = messageButton;
}
@Override
public String toString() {
return "KakaoMessage{" +
"text='" + text + '\'' +
", photo=" + photo +
", messageButton=" + messageButton +
'}';
}
}
1-2. Request
public class KakaoMessageRequest {
@JsonProperty("user_key")
private String userKey;
private String type;
private String content;
public String getUserKey() {
return userKey;
}
public void setUserKey(String userKey) {
this.userKey = userKey;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Override
public String toString() {
return "KakaoMessageRequest{" +
"userKey='" + userKey + '\'' +
", type='" + type + '\'' +
", content='" + content + '\'' +
'}';
}
}
1-3. Response
public class KakaoMessageResponse {
private KakaoMessage message;
private KakaoKeyboard keyboard;
public KakaoMessage getMessage() {
return message;
}
public void setMessage(KakaoMessage message) {
this.message = message;
}
public KakaoKeyboard getKeyboard() {
return keyboard;
}
public void setKeyboard(KakaoKeyboard keyboard) {
this.keyboard = keyboard;
}
@Override
public String toString() {
return "KakaoMessageResponse{" +
"message=" + message +
", keyboard=" + keyboard +
'}';
}
}
1-4. Controller
@RestController
@RequestMapping(value = "/kakaobot")
public class KakaoBotController {
static final Logger LOG = LoggerFactory.getLogger(KakaoBotController.class);
static final String STR_SEPARATOR = "\n";
@Autowired
private RestaurantService restaurantService;
@RequestMapping(value = "/keyboard", method = RequestMethod.GET)
public ResponseEntity<KakaoKeyboard> handleKeyboard () {
return new ResponseEntity<> (makeKakaoKeyboard(), HttpStatus.OK);
}
@RequestMapping(value = "/message", method = RequestMethod.POST)
public ResponseEntity<KakaoMessageResponse> handleMessage (
@RequestBody KakaoMessageRequest request) throws Exception {
LOG.info("request : {}", request);
KakaoMessageResponse response = new KakaoMessageResponse();
KakaoMessage kakaoMessage = new KakaoMessage();
String strMessage;
String[] arrText = request.getContent().split(" ");
int commandCount = arrText.length;
if (commandCount == 1) {
String command1 = arrText[0].trim();
List<String> searchWordList = restaurantService.findSearchWordList();
if (command1.equals("음식")
|| command1.equals("점심")
|| command1.equals("뭐먹지")
|| command1.equals("뭐먹을래")) {
if (CollectionUtils.isEmpty(searchWordList)) {
strMessage = "음식종류가 없습니다.";
}
else {
strMessage = StringUtils.join(searchWordList, STR_SEPARATOR);
}
}
else {
if (CollectionUtils.isEmpty(searchWordList)) {
strMessage = StringUtils.join(restaurantService.errorMessage(), STR_SEPARATOR);
}
else {
if (searchWordList.contains(command1)) {
strMessage = StringUtils.join(restaurantService.choiceRandomRestaurant(command1, "역삼"), STR_SEPARATOR);
}
else {
strMessage = StringUtils.join(restaurantService.errorMessage(), STR_SEPARATOR);
}
}
}
}
else if (commandCount == 2) {
String command1 = arrText[0].trim();
String command2 = arrText[1].trim();
switch (command2) {
// 식당 전체정보
case "리스트":
case "어디":
case "가게":
case "식당":
strMessage = StringUtils.join(restaurantService.findRestaurantList(command1, "역삼"), STR_SEPARATOR);
break;
// 식당 상세정보
case "정보":
case "상세":
case "뭐팔아":
case "뭐팔어":
strMessage = StringUtils.join(restaurantService.findRestaurantDetail(command1), STR_SEPARATOR);
break;
// 식당 좋아요
case "+":
case "++":
case "좋아":
case "좋아요":
case "맛있어":
case "맛나요":
strMessage = StringUtils.join(restaurantService.plusLike(command1), STR_SEPARATOR);
break;
// 식당 싫어요 (3진 아웃)
case "-":
case "--":
case "싫어":
case "싫어요":
case "맛없어":
strMessage = StringUtils.join(restaurantService.plusHate(command1), STR_SEPARATOR);
break;
default:
strMessage = StringUtils.join(restaurantService.errorMessage(), STR_SEPARATOR);
break;
}
}
else {
strMessage = StringUtils.join(restaurantService.errorMessage(), STR_SEPARATOR);
}
kakaoMessage.setText(strMessage);
response.setMessage(kakaoMessage);
response.setKeyboard(makeKakaoKeyboard());
return new ResponseEntity<>(response, HttpStatus.OK);
}
private KakaoKeyboard makeKakaoKeyboard () {
KakaoKeyboard kakaoKeyboard = new KakaoKeyboard();
kakaoKeyboard.setType("text");
return kakaoKeyboard;
}
}
2. 플러스친구 앱 설정
2-1. 스마트채팅 설정
1) API형 설정

2) 앱 등록
- 앱 이름 : 점심 뭐 먹지 (역삼역)
- 앱 URL : https://ssingssing2.com/kakaobot
- 앱 설명 : 점심 메뉴 추천

3) API 사용 설정



2-2. 앱 공개 설정

2-3. 카카오톡 플러스 친구 등록




'Bot > KaKao Bot' 카테고리의 다른 글
Kakaotalk BOT - 1. 계정 만들기 (0) | 2020.02.06 |
---|