Skip to content
This repository has been archived by the owner on Dec 22, 2020. It is now read-only.

Commit

Permalink
cleaner code to fetch chat from db
Browse files Browse the repository at this point in the history
  • Loading branch information
albertoxamin committed Mar 13, 2019
1 parent a0a688d commit 5d307cf
Showing 1 changed file with 59 additions and 74 deletions.
133 changes: 59 additions & 74 deletions bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ const KEYBOARD_NOTIFICATIONS = (chat) => Markup.inlineKeyboard([
Markup.callbackButton(`Intero ${(chat.subMenu ? '✅' : '❌')}`, 'not_menu')
]).extra()

const getChat = (chatId, callback, notFoundCallback) => {
Chat.findOne({ chatId: chatId }, function (err, chat) {
if (err) return console.log(err)
if (chat) {
callback(chat)
} else if (notFoundCallback) {
notFoundCallback()
} else {
console.log('ERROR: chat is null on the db')
}
})
}

bot.telegram.getMe().then((bot_informations) => {
bot.options.username = bot_informations.username
console.log('Server has initialized bot nickname. Nick: ' + bot_informations.username)
Expand Down Expand Up @@ -124,45 +137,31 @@ bot.on('inline_query', async ({ inlineQuery, answerInlineQuery }) => {
bot.on('callback_query', (ctx) => {
console.log(ctx.callbackQuery)
if (ctx.callbackQuery.data.indexOf('not_') != -1) {
Chat.findOne({ chatId: ctx.callbackQuery.message.chat.id.toString() }, function (err, chat) {
if (err) {
console.log(err)
return
}
if (chat) {
if (ctx.callbackQuery.data == 'not_lesto')
chat.subLesto = !(chat.subLesto)
else if (ctx.callbackQuery.data == 'not_menu')
chat.subMenu = !(chat.subMenu)
chat.save(function (err, obj) {
if (err) {
console.log('Error: ' + err)
}
telegram.editMessageText(ctx.callbackQuery.message.chat.id, ctx.callbackQuery.message.message_id, null,
'Ti invierò un messaggio ogni giorno, scegli il menù che vuoi ricevere\n(toccando nuovamente il menù non riceverai più le notifiche)',
KEYBOARD_NOTIFICATIONS(obj))
ctx.answerCbQuery('Impostazioni aggiornate!')
})
} else {
console.log('ERROR: chat is null on the db')
}
getChat(ctx.callbackQuery.message.chat.id.toString(), (chat) => {
if (ctx.callbackQuery.data == 'not_lesto')
chat.subLesto = !(chat.subLesto)
else if (ctx.callbackQuery.data == 'not_menu')
chat.subMenu = !(chat.subMenu)
chat.save(function (err, obj) {
if (err) {
console.log('Error: ' + err)
}
telegram.editMessageText(ctx.callbackQuery.message.chat.id, ctx.callbackQuery.message.message_id, null,
'Ti invierò un messaggio ogni giorno, scegli il menù che vuoi ricevere\n(toccando nuovamente il menù non riceverai più le notifiche)',
KEYBOARD_NOTIFICATIONS(obj))
ctx.answerCbQuery('Impostazioni aggiornate!')
})
})
}
})

bot.command('notifiche', (ctx) => {
logAction(ctx, 'Setting notifications ')
Chat.findOne({ chatId: ctx.message.chat.id.toString() }, function (err, chat) {
if (err) {
console.log(err)
return
}
if (chat) {
return ctx.replyWithMarkdown('Ti invierò un messaggio ogni giorno, scegli il menù che vuoi ricevere\n(toccando nuovamente il menù non riceverai più le notifiche)', KEYBOARD_NOTIFICATIONS(chat))
} else {
console.log('ERROR: chat is null on the db')
}
})
getChat(ctx.message.chat.id.toString(),
(chat) =>
ctx.replyWithMarkdown('Ti invierò un messaggio ogni giorno, scegli il menù che vuoi ricevere\n' +
'(toccando nuovamente il menù non riceverai più le notifiche)',
KEYBOARD_NOTIFICATIONS(chat)))
})

bot.command('setholiday', (ctx) => {
Expand Down Expand Up @@ -202,23 +201,15 @@ bot.command('say', (ctx) => {

bot.command('stop', (ctx) => {
console.log('stopped by ' + ctx.message.chat.username)
Chat.findOne({ chatId: ctx.message.chat.id.toString() }, function (err, chat) {
if (err) {
console.log(err)
return
}
if (chat) {
chat.subLesto = false
chat.subMenu = false
chat.isBotBlocked = true
chat.save(function (err, obj) {
if (err) {
console.log('Error: ' + err)
}
})
} else {
console.log('ERROR: chat is null on the db')
}
getChat(ctx.message.chat.id.toString(), (chat) => {
chat.subLesto = false
chat.subMenu = false
chat.isBotBlocked = true
chat.save(function (err, obj) {
if (err) {
console.log('Error: ' + err)
}
})
})
})

Expand All @@ -228,33 +219,27 @@ function logAction(ctx, actionMessage) {
else {
console.log(moment().format() + ' ' + actionMessage + ' on ' + ctx.chat.id + ' aka @' + ctx.message.chat.username)
}
Chat.findOne({ chatId: ctx.chat.id }, function (err, chat) {
if (err) {
console.log(err)
return
}
if (chat) {
if (chat.isBotBlocked) {
chat.isBotBlocked = false
chat.save(function (err, obj) {
if (err) {
console.log('Error: ' + err)
}
return
})
}
return
} else {
let newChat = new Chat()
newChat.chatId = ctx.chat.id
newChat.subLesto = false
newChat.subMenu = false
newChat.save(function (err, obj) {
if (err)
console.log(err)
getChat(ctx.chat.id, (chat) => {
if (chat.isBotBlocked) {
chat.isBotBlocked = false
chat.save(function (err, obj) {
if (err) {
console.log('Error: ' + err)
}
return
})
}
return
}, () => {
let newChat = new Chat()
newChat.chatId = ctx.chat.id
newChat.subLesto = false
newChat.subMenu = false
newChat.save(function (err, obj) {
if (err)
console.log(err)
return
})
})
}

Expand Down

0 comments on commit 5d307cf

Please sign in to comment.