-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
67 lines (51 loc) · 1.63 KB
/
app.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
// Imports dependencies and set up http server
const Koa = require('koa'),
Router = require('koa-router'),
koaBody = require('koa-body'),
session = require('telegraf/session'),
app = new Koa(),
router = new Router(),
//Import helper functions
helper = require('./helpers')
spotifySearch = require('./spotifyConfig')
app.use(koaBody())
app.use(router.routes())
require('dotenv').config()
// Url and token for connect to Telegram API
const token = process.env.ACCESS_TOKEN
const url = process.env.URL
const port = parseInt(process.env.PORT || '3005', 10)
// Imports and set up framework for bot
const Telegraf = require('telegraf'),
bot = new Telegraf(token)
bot.use(session())
// If method is POST call handleUpdate function which just part of Telegraf API
// and handle raw Telegram update
app.use((ctx, next) => ctx.method === 'POST' || ctx.url === '/emo_marvin' ?
bot.handleUpdate(ctx.request.body, ctx.response) :
next())
// Just some home page
router.get('/', ctx => {
console.log(ctx)
ctx.body = 'emo Marvin lives here';
})
// Set up webhook
bot
.telegram
.setWebhook(url);
// Bot shills
bot.start(({
reply
}) => reply('wanna some real sad stuff? send \'song\' to get some emo song'))
bot.hears('song', async (ctx) => {
const trackUrl = await spotifySearch()
ctx.reply(helper.random(trackUrl, trackUrl.length))
})
// Error handle
bot.catch((err) => {
console.log('hmmmm, something wrong', err)
})
// Listening port
app.listen(port, () => {
console.log('emo Marvin is here')
})