-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbot.js
46 lines (41 loc) · 2.52 KB
/
bot.js
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
const Telegraf = require('telegraf');
const config = require('./config'); // Holds Telegram API token plus YouTube API token
var youtube = require('./models/youtube'); // Provides easy access to YouTube API
const bot = new Telegraf(config.telegraf_token);
bot.telegram.getMe().then((bot_informations) => {
bot.options.username = bot_informations.username;
console.log("Server has initialized bot nickname. Nick: "+bot_informations.username);
});
// Usage: @yourbot channel. Retrieves: a list of last videos of 'channel' query input.
bot.on('inline_query', ctx => {
let nickname = ctx.update.inline_query.query; // Take the nickname out of Telegraf context structure.
if(nickname.length > 3){ // If user input is longer than 3 characters
// Search channel based on nickname. If there is one with same user query, let's retrieve its channel ID.
youtube.get_id_from_nickname(nickname).then(channel_id => {
// Let's get last uploaded videos as data structure through YouTube API (thanks to our written model).
youtube.fetch_channel_uploads(channel_id).then(structured_data => {
// Let's parse those structured data to get only essential informations for video listing.
youtube.parse_list_videos(structured_data).then(video_info => {
// Let's encapsulate those informations in a list that Telegraf API can digest.
let new_arr = [];
// For each video, extract retrieved informations for extra elaborations.
for(let k in video_info){
new_arr[k] = {
type: 'video',
id: k,
title: video_info[k].title,
description: video_info[k].description,
video_url: video_info[k].url,
mime_type: "video/mp4",
thumb_url: video_info[k].thumb,
input_message_content: {message_text: "[BOT] "+video_info[k].url}
}
}
// Let's show this list to the user. Cache time is set to zero for development purposes.
return ctx.answerInlineQuery(new_arr, {cache_time: 0});
}).catch(error => {console.log("Promise error: "+error)});
}).catch(error => {console.log("Promise error: "+error)});
}).catch(error => {console.log("Promise error: "+error)});
}
});
bot.startPolling();