line.ctrl.js 4.15 KB
const request = require('request');
const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'

require("dotenv").config();
const TOKEN = process.env.LINE_CHANNEL_KEY;

const Profile = require("../../models/profile");
const problem_set = require("../../data/problem_set");
const compareBJ = require("../../util/compareBJ");

let username = "";

exports.linestart = async(ctx) => {
  var eventObj = ctx.request.body.events[0];
 
  // request log
  console.log('======================', new Date() ,'======================');
  console.log('[request]', ctx.request.body);
  console.log('[request source] ', eventObj.source);
  console.log('[request message]', eventObj.message);
  console.log('[username]', username);

  if(username == "") {
    username = eventObj.message.text;

    const result = await isExist(username);
    if (!result) {
        username = "";
    
        request.post(
          {
              url: TARGET_URL,
              headers: {
                  'Authorization': `Bearer ${TOKEN}`
              },
              json: {
                  "replyToken":eventObj.replyToken,
                  "messages":[
                      {
                          "type":"text",
                          "text": "작심삼일 혹은 백준 사이트의 ID값이 올바르지 않습니다. 다시 입력해주세요."
                      }
                  ]
              }
          },(body) => {
              console.log(body)
          });
   } else {
	request.post({
              url: TARGET_URL,
              headers: {
                  'Authorization': `Bearer ${TOKEN}`
              },
              json: {
                  "replyToken":eventObj.replyToken,
                  "messages":[
                      {
                          "type":"text",
                          "text": eventObj.message.text + " : 정상 등록 되었습니다."
                      }
                  ]
              }
          },(body) => {
              console.log(body)
          });

	}
  };


  if(eventObj.message.text == "reset ID") {
    username = "";
    request.post(
      {
          url: TARGET_URL,
          headers: {
              'Authorization': `Bearer ${TOKEN}`
          },
          json: {
              "replyToken": eventObj.replyToken,
              "messages":[
                  {
                      "type":"text",
                      "text": "작심삼일 혹은 백준 사이트의 ID가 초기화 되었습니다."
                  }
              ]
          }
      },(body) => {
          console.log(body)
      });
  }
  

  if(eventObj.message.text == "문제") {
    console.log("문제를 추천합니다.");
    let recommendData = await lineRecommend(username);
    recommendBJ(eventObj.replyToken, recommendData);
    
    console.log(recommendData);
  }

}

function recommendBJ(replyToken, recommendData) {
  var recommendProblem = "오늘의 추천문제는 " + recommendData.problem_title + "입니다.";
  var problemURL = "https://www.boj.kr/" + recommendData.problem_number;
  request.post(
      {
          url: TARGET_URL,
          headers: {
              'Authorization': `Bearer ${TOKEN}`
          },
          json: {
              "replyToken":replyToken,
              "messages":[
                  {
                      "type":"text",
                      "text": recommendProblem
                  },
                  {
                      "type":"text",
                      "text": problemURL
                  }
              ]
          }
      },(body) => {
          console.log(body)
      });
}

async function lineRecommend (username) {

    const profile = await Profile.findByUsername(username);
    if (!profile) {
      console.log("401");
      return;
    }

    const unsolved_data = await compareBJ.compareBJ(
      profile.getBJdata(),
      problem_set.problem_set
    );

    const recommendData = await compareBJ.randomItem(unsolved_data);

    if (!recommendData) {
      console.log("402");
      return;
    } 

    return recommendData;
    
};

async function isExist(username) {
    const profile = await Profile.findByUsername(username);
    
    if (!profile) {
      return false;
    }
    
    return true;
}