video_sad.js 2.03 KB
var Info = require('./Info');

exports.check = function(message,replyToken){

    const rand_0to9 = Math.floor(Math.random() * 10);   // 0~9까지 난수생성
    var ids = ['p5iu1V30myk', 'i-SBnhaZSSU', 'Etfgb6A7hSI', 'IN2mml9xs_s', // 모든 동영상 id를 관리하는 배열
        'tp1uoFAfgHE', 'Qx22TnVVIfU', '3y1QIqR115A', 'VXBI1wt8XII', 'aWMBn2--E0Q', 'HyTbgBlnLCo'];

    const request = require('request');
    const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
    
    
    var { google } = require('googleapis');

    var service = google.youtube('v3');
    service.videos.list
    (
        {
        key: 'AIzaSyBInggOtXxPFYIRee0Xs3vb5iZ9YE9_518',
        part: 'snippet',
        id: ids[rand_0to9], // 동영상 Id random input
        fields: 'items(snippet(title))'
        },
        function (err, response)
        {
            if (err)
            {
                console.log('The API returned an error: ', err);
                return;
            }

            var video = response.data.items;
            
            if (video.length == 0)
            {
                console.log('검색된 동영상이 없습니다.');
            }
            else
            {
                sendMessage = JSON.stringify(response.data.items[0].snippet.title);
                console.log(sendMessage);
                request.post(
                    {
                        url: TARGET_URL,
                        headers:
                        {
                            'Authorization': `Bearer ${Info.TOKEN}`
                        },
                        json:
                        {
                            "replyToken": replyToken,
                            "messages":
                            [
                                {
                                    "type": "text",
                                    "text": sendMessage
                                }
                            ]
                        }
                    }
                );
            }
        }
    )





}