searchApp.js 전체 코드

이 글은 2024-08-09에 최종 업데이트 되었습니다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
const axios = require("axios");
const querystring = require("querystring");
const { TeamsActivityHandler, CardFactory } = require("botbuilder");
const ACData = require("adaptivecards-templating");
const helloWorldCard = require("./adaptiveCards/helloWorldCard.json");
const pokemonCard = require("./adaptiveCards/pokemonCard.json");
const { Console } = require("console");
const { type } = require("os");

function search(nameKey, myArray, returnVal){
  for (let i=0; i < myArray.length; i++) {
      if (myArray[i].name === nameKey) {
        if (returnVal){
          return myArray[i].value;
        }
        else{
          return myArray[i];
        }
      }
  }
}

function timestamp(){
  var today = new Date();
  today.setHours(today.getHours() + 9);
  return today.toISOString().replace('T', ' ').substring(0, 19);
}

class SearchApp extends TeamsActivityHandler {
  constructor() {
    super();
  }

  // Message extension Code
  // Search.
  async handleTeamsMessagingExtensionQuery(context, query) {
    //const searchQuery = query.parameters[0].value;
    console.log(search('poke_name', query.parameters, false));
    console.log(search('poke_type', query.parameters, false));
    console.log(timestamp());
    
    let poke_name = "", poke_type = "";
    if (search('poke_name', query.parameters, false)){
      poke_name = search('poke_name', query.parameters, false).value;
    }
    if (search('poke_type', query.parameters, false)){
      poke_type = search('poke_type', query.parameters, false).value;
    }

    const response = await axios.get(
      `https://pogokrapi.azurewebsites.net/api/pogo?${querystring.stringify({
        name: poke_name,
        type: poke_type,
      })}`
    );

    const attachments = [];
    response.data.pokemons.forEach((pokemon) => {
      const evols = [];
      if (pokemon.prev_evolution){
        pokemon.prev_evolution.forEach((evol) => {
          evols.push(evol);
        })
      }
      evols.push({
        "num":pokemon.num,
        "name":pokemon.name
      });
      if (pokemon.next_evolution){
        pokemon.next_evolution.forEach((evol) => {
          evols.push(evol);
        })
      }

      let temp_evolimg1 = "", temp_evolimg2 = "", temp_evolimg3 = "", temp_evolimg4 = ""
      let temp_evolname1 = "", temp_evolname2 = "", temp_evolname3 = "", temp_evolname4 = "";
      if (evols[0]) {
        temp_evolimg1 = "http://www.serebii.net/pokemongo/pokemon/" + evols[0].num + ".png";
        temp_evolname1 = evols[0].name;
      }
      if (evols[1]) {
        temp_evolimg2 = "http://www.serebii.net/pokemongo/pokemon/" + evols[1].num + ".png";
        temp_evolname2 = evols[1].name;
      }
      if (evols[2]) {
        temp_evolimg3 = "http://www.serebii.net/pokemongo/pokemon/" + evols[2].num + ".png";
        temp_evolname3 = evols[2].name;
      }
      if (evols[3]) {
        temp_evolimg4 = "http://www.serebii.net/pokemongo/pokemon/" + evols[3].num + ".png";
        temp_evolname4 = evols[3].name;
      }

      const template = new ACData.Template(pokemonCard);
      const card = template.expand({
        $root: {
          num: pokemon.num,
          name: pokemon.name,
          img: pokemon.img,
          type: pokemon.type,
          height: pokemon.height,
          weight: pokemon.weight,
          candy: pokemon.candy,
          weaknesses: pokemon.weaknesses.join(", "),
          evolimg1: temp_evolimg1,
          evolname1: temp_evolname1,
          evolimg2: temp_evolimg2,
          evolname2: temp_evolname2,
          evolimg3: temp_evolimg3,
          evolname3: temp_evolname3,
          evolimg4: temp_evolimg4,
          evolname4: temp_evolname4,
        },
      });
      const preview = CardFactory.heroCard(pokemon.name);
      const attachment = { ...CardFactory.adaptiveCard(card), preview };
      attachments.push(attachment);
    });

    return {
      composeExtension: {
        type: "result",
        attachmentLayout: "list",
        attachments: attachments,
      },
    };
  }
}

module.exports.SearchApp = SearchApp;