Skip to content

Commit

Permalink
Merge pull request #1 from lluisd/birthdays
Browse files Browse the repository at this point in the history
notify birthday
  • Loading branch information
lluisd authored Feb 20, 2024
2 parents 143c9eb + 719e90a commit 5d0679f
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 21 deletions.
1 change: 0 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const Messenger = require('./lib/messenger')
const mongoose = require('mongoose')
const config = require('./config')
const express = require("express")
const Notifier = require('./lib/notifier')

mongoose.connect(config.database).then(() => {
const messenger = new Messenger()
Expand Down
2 changes: 1 addition & 1 deletion handlers/birthday.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Birthday {
}

_getText(model) {
return `@${model.nick} cumple el dia ${model.day} de ${monthTexts[model.month -1]}`
return `${model.nick} cumple el dia ${model.day} de ${monthTexts[model.month -1]}`
}

_getDay (text) {
Expand Down
42 changes: 31 additions & 11 deletions handlers/stream.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const TwitchService = require('../services/twitch')
const BrowserService = require('../services/browser')
const BirthdayService = require('../services/birthday')
const config = require("../config")
const moment = require('moment')
require('moment-precise-range-plugin')
Expand All @@ -25,24 +26,21 @@ class Stream {
}
}

async catchStream (bot) {
async catchStream (telegramBot, twitchBot, target) {
const result = await TwitchService.getStream()

if (result && result.type === 'live' ) {
await this.sendTodayBirthday(twitchBot, target)
const text = this._getText(result)

const options = {
parse_mode: 'Markdown'
}

const msg = await bot.sendMessage(config.telegram.chatId, text, options)
await bot.pinChatMessage(config.telegram.chatId, msg.message_id).catch(() => {})
const options = { parse_mode: 'Markdown' }
const msg = await telegramBot.sendMessage(config.telegram.chatId, text, options)
await telegramBot.pinChatMessage(config.telegram.chatId, msg.message_id).catch((err) => { console.error(`cannot pin chat: ${err}`)})
await TwitchService.saveLastMessage(msg)
await TwitchService.saveTitle(result.title)
await BrowserService.startAndWarmUpBrowserIfNeeded().catch(() => { console.error('startAndWarmUpBrowserIfNeeded on live')})

await BrowserService.startAndWarmUpBrowserIfNeeded().catch(() => { console.error('startAndWarmUpBrowserIfNeeded on live')})
} else if (result && result.type === 'finished' && result.messageId) {
await bot.deleteMessage(config.telegram.chatId, result.messageId)
await telegramBot.deleteMessage(config.telegram.chatId, result.messageId)
await TwitchService.deleteLastMessage()
await TwitchService.saveTitle(result.title)
await BrowserService.closeBrowser().catch(() => { console.error('closeBrowser on finished')})
Expand All @@ -52,7 +50,7 @@ class Stream {
message_id: result.messageId,
parse_mode: 'Markdown'
}
await bot.editMessageText(this._getText(result), options).catch(() => {})
await telegramBot.editMessageText(this._getText(result), options).catch(() => {})
await TwitchService.saveTitle(result.title)
await BrowserService.startAndWarmUpBrowserIfNeeded().catch(() => { console.error('startAndWarmUpBrowserIfNeeded on stillLive')})
} else if (result && result.type === 'notLive') {
Expand All @@ -74,6 +72,28 @@ class Stream {
const title = `🔴 *¡EN DIRECTO!*`
return `${image} ${title} ${link} \n _${stream.title}_ (${duration})`
}

async sendTodayBirthday(twitchBot, target) {
const bdays = await BirthdayService.getTodayBirthdays()
if (bdays && bdays.length > 0) {
let text
if (bdays.length === 1){
text = this._getTodayBdayText(`${bdays[0].nick}`)
} else if (bdays.length > 1) {
const nicks = bdays.map(bday => `${bday.nick}`).join(', ').replace(/, ([^,]*)$/, ' y $1')
text = this._getTodayBdaysText(nicks)
}
twitchBot.say(target, text)
}
}

_getTodayBdaysText(nicks) {
return ${nicks} cumplen años hoy!`
}

_getTodayBdayText(nick) {
return ${nick} cumple años hoy!`
}
}

module.exports = Stream
9 changes: 8 additions & 1 deletion helpers/dbmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const Token = require('../models/token')
const Muncipio = require('../models/municipio')
const Channel = require('../models/channel')
const Birthday = require('../models/birthday')
const moment = require('moment')


function getToken (userId) {
return Token.findOne({userId: userId})
Expand Down Expand Up @@ -46,6 +48,10 @@ async function updateBirthday (nick, update) {
}).lean()
}

async function getBirthdayFromDate(day, month) {
return Birthday.find({day: day, month: month}).lean()
}

module.exports = {
getToken,
updateToken,
Expand All @@ -56,5 +62,6 @@ module.exports = {
getChannel,
updateChannel,
updateBirthday,
getBirthday
getBirthday,
getBirthdayFromDate
}
10 changes: 6 additions & 4 deletions lib/messenger.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const InputParser = require('./inputParser')
const inputParser = new InputParser()
const tmi = require('@twurple/auth-tmi')
const { RefreshingAuthProvider } = require('@twurple/auth')
const TokenService = require("../services/token");
const Notifier = require("./notifier");
const TokenService = require("../services/token")
const Notifier = require("./notifier")

class Messenger {
constructor () {}
Expand All @@ -21,7 +21,7 @@ class Messenger {
}
this.bot = new tmi.client(opts)

this.notifier = new Notifier()
this.notifier = new Notifier(this.bot)
await this.notifier.notify()

return this.listen()
Expand Down Expand Up @@ -50,6 +50,8 @@ class Messenger {
this.bot.on('hosting', this.handleHosting.bind(this));

return this.bot.connect().catch(console.error)


}

handleText (target, context, msg, self) {
Expand Down Expand Up @@ -101,7 +103,7 @@ class Messenger {
}

handleConnect (addr, port) {
console.log(`* Connected to ${addr}:${port}`);
console.log(`* Connected to ${addr}:${port}`)
}
}

Expand Down
11 changes: 9 additions & 2 deletions lib/notifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,22 @@ const cron = require('node-cron')
const handlers = require('../handlers')

class Notifier {
constructor () {
constructor (twitchBot) {
this.target = `#${config.twitch.channels}`
this.twitchBot = twitchBot
this.bot = new TelegramBot(config.telegram.apiKey)
}

notify () {
cron.schedule('*/1 * * * *', () => {
handlers.stream.catchStream(this.bot)
handlers.stream.catchStream(this.bot, this.twitchBot, this.target)
})

cron.schedule('0 * * * *', () => {
handlers.stream.sendTodayBirthday(this.twitchBot, this.target)
})


return Promise.resolve()
}

Expand Down
11 changes: 10 additions & 1 deletion services/birthday.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const dbManager = require('../helpers/dbmanager')
const moment = require('moment')

async function getBirthday(nick) {
let result = null
Expand All @@ -10,8 +11,16 @@ async function addBirthday(nick, day, month) {
return await dbManager.updateBirthday(nick, {day: day, month: month})
}

async function getTodayBirthdays() {
let result = null
const today = moment()
result = await dbManager.getBirthdayFromDate(today.date(), today.month() + 1)
return result
}


module.exports = {
getBirthday,
addBirthday
addBirthday,
getTodayBirthdays
}

0 comments on commit 5d0679f

Please sign in to comment.