-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
112 lines (93 loc) · 3.13 KB
/
index.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
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
import fetchAlbum from "./lib/album.js";
import { fetchSources, search } from "./lib/search.js";
import { Markup, Telegraf } from 'telegraf'
import * as messages from './lib/messages.js'
import 'dotenv/config'
import express from 'express'
const token = process.env.BOT
const bot = new Telegraf(token)
const sources = await fetchSources()
const app = express()
const port = process.env.PORT || 5000
const url = "https://pure-bgm-bot.herokuapp.com"
bot.telegram.setWebhook(`${url}/bot${token}`)
app.use(bot.webhookCallback(`/bot${token}`))
bot.on('inline_query', async (context) => {
let query = context.inlineQuery.query
let res = await search(query, sources)
let results = res.splice(0, 10).map((result, index) => {
return {
'type': 'article',
'title': result.item.parsedURL,
'id': index.toString() + "-id",
'message_text': "* " + result.item.parsedURL
}
})
return await context.answerInlineQuery(results)
})
bot.start(context => {
context.replyWithMarkdown(messages.welcomeMessage,
Markup.inlineKeyboard([
Markup.button.switchToCurrentChat('🔍 Search', ' '),
Markup.button.callback('🧑💻 Developer', 'dev'),
Markup.button.callback('ℹ️ Info', 'info')
])
)
})
bot.action('dev', (context) => {
return context.replyWithMarkdown(messages.aboutDeveloperMessage)
})
bot.action('info', (context) => {
return context.replyWithMarkdown(messages.infoMessage)
})
bot.on('text', async (context) => {
let text = context.message.text
if (text.startsWith('*')) {
let arr = text.split(" ")
arr.shift()
let query = arr.join(" ")
let des = sources.filter(s => s.parsedURL === query)[0]
let target = des.url
fetchAlbum(target)
.then(async (data) => {
try {
await context.replyWithPhoto(data.poster, {
'caption': data.title
})
}
catch (error) {
console.log(error)
}
data.bgms.forEach(async (bgm) => {
await context.replyWithAudio(bgm.bgmSource, {
'caption': bgm.bgmName
})
})
await context.replyWithMarkdown(
`🔍 Search your next Album`,
Markup.inlineKeyboard([
Markup.button.switchToCurrentChat('🔍 Search', ' ')
])
)
})
} else {
context.reply('Only inline Search')
context.replyWithMarkdown(
`🔍 Search`,
Markup.inlineKeyboard([
Markup.button.switchToCurrentChat('🔍 Search', ' ')
])
)
}
})
bot.catch(err => {
console.log(err)
bot.telegram.sendMessage(1047531822, "err")
})
app.get('/', (_, res) => {
res.send('Hello World')
})
app.listen(port, () => { })
// bot.launch()
// process.once('SIGINT', () => bot.stop('SIGINT'))
// process.once('SIGTERM', () => bot.stop('SIGTERM'))