video_sad.js 2.58 KB
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 fs = require('fs');
    const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
    //라인 고유 토큰
    const TOKEN = 'Kb1/rQYz4MUhF8XyKQv7z9x0MxVQ5bX/XO8S/yt/1qQEJVAbsEFAaMvXKEOx9Umr7KhivfyDPfZHRRLFPngR0O4ZGWV2VFses8ufPE7uAdvYr4G6keBNAU69nBz5IC71HfbIrUHxXYqD7GfhVwXzpwdB04t89/1O/w1cDnyilFU='
    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 ${TOKEN}`
                        },
                        json:
                        {
                            "replyToken": replyToken,
                            "messages":
                            [
                                {
                                    "type": "image",
                                    "originalContentUrl": "https://img.youtube.com/vi/"+ids[rand_0to9]+"/hqdefault.jpg",
                                    "previewImageUrl": "https://img.youtube.com/vi/"+ids[rand_0to9]+"/hqdefault.jpg"
                                },
                                {
                                    "type": "text",
                                    "text": sendMessage
                                }
                            ]
                        }
                    }
                );
            }
        }
    )





}