diff --git a/requests.js b/requests.js index a605fa65b5375cab230bd2d3df101f5b0c5113ef..020819bcbaa994ac0ddf63ce4c21e5784baae422 100644 --- a/requests.js +++ b/requests.js @@ -28,12 +28,14 @@ async function sendRequest(req, chan) { // Récupération de tous les personnes et leurs assos ayant leur anniversaire function getBirthdays(chan) { - const req = 'query getUsersBirthday {users: usersBirthday { ...userData}}fragment userData on User {firstName lastName roles {sector {composition {id}}}}' + const req = 'query getUsersBirthday {users: usersBirthday { ...userData}}fragment userData on User {firstName lastName login promotion roles {sector {composition {id}}}}' return sendRequest(req, chan).then(body => { const users = []; body.data.users.forEach(user => { use = {}; use.name = `${user.firstName} ${user.lastName}`; + use.login = user.login; + use.promo = user.promotion; use.asso = []; user.roles.forEach(role => { use.asso.push(role.sector.composition.id); diff --git a/schedule.js b/schedule.js index 2dff4ca43b9e03fec32cc60290092d8b1e887d4d..6a445dbe5698fd320b53c22bfede8af254bdf809 100644 --- a/schedule.js +++ b/schedule.js @@ -26,11 +26,19 @@ function addSchedule(chan, time, bot) { // récupère que les personnes du jour qui font partie des groupes ciblés const newUsers = users.filter(user => user.asso.some(asso => groups.indexOf(asso) !== -1)); if (newUsers.length === 0) return - var msg = '**Joyeux anniversaire** à :\n' - newUsers.forEach(user => { - msg += `${user.name}\n` - }); - return bot.sendMessage(chan.chatId, msg, { parse_mode: 'Markdown' }); + if (chan.chatId > 0) { // chan privé + var msg = 'N\'oubliez pas les <b>anniversaires</b> de :\n' + } else { // chan à plusieurs + var msg = '<b>Joyeux anniversaire</b> à :\n' + } + users.forEach((user, index, array) => { + if (index !== array.length - 1) { + msg += `├ <a href = "https://linkcs.fr/user/${user.login}">${user.name}</a>\n` + } else { + msg += `└ <a href = "https://linkcs.fr/user/${user.login}">${user.name}</a>\n` + } + }) + return bot.sendMessage(chan.chatId, msg, { parse_mode: 'HTML' }); }) }) }) diff --git a/telegram.js b/telegram.js index 39f9de160d030b93cc8b8af674b36e961c0ab3c5..56edf906fe6bddb2021e9a127734b4f9e1d5de09 100644 --- a/telegram.js +++ b/telegram.js @@ -100,11 +100,20 @@ bot.onText(/\/allbirthdays/, msg => { getChanByChatId(chatId).then(chan => { return getBirthdays(chan) }).then(users => { - var msg = '**Joyeux anniversaire** à :\n' - users.forEach(user => { - msg += `${user.name}\n` - }); - bot.sendMessage(chatId, msg, { parse_mode: 'Markdown' }); + if (chatId > 0) { // chan privé + var msg = 'N\'oubliez pas les <b>anniversaires</b> de :\n' + } else { // chan à plusieurs + var msg = '<b>Joyeux anniversaire</b> à :\n' + } + users.sort((use1, use2) => use1.promo - use2.promo); + users.forEach((user, index, array) => { + if (index !== array.length - 1) { + msg += `├ <a href = "https://linkcs.fr/user/${user.login}">${user.name}</a>\n` + } else { + msg += `└ <a href = "https://linkcs.fr/user/${user.login}">${user.name}</a>\n` + } + }) + return bot.sendMessage(chatId, msg, { parse_mode: 'HTML' }); }) }); @@ -115,17 +124,26 @@ bot.onText(/\/birthdays/, msg => { getChanByChatId(chatId).then(chan => { return Promise.all([ getBirthdays(chan), - getCompos(chatId), + getCompos(chatId) ]) }).then(([users, groups]) => { // récupère que les personnes du jour qui font partie des groupes ciblés const newUsers = users.filter(user => user.asso.some(asso => groups.indexOf(asso) !== -1)); if (newUsers.length === 0) return bot.sendMessage(chatId, 'Pas d\'anniversaire à souhaiter aujourd\'hui.') - var msg = '**Joyeux anniversaire** à :\n' - newUsers.forEach(user => { - msg += `${user.name}\n` - }); - return bot.sendMessage(chatId, msg, { parse_mode: 'Markdown' }); + if (chatId > 0) { // chan privé + var msg = 'N\'oubliez pas les <b>anniversaires</b> de :\n' + } else { // chan à plusieurs + var msg = '<b>Joyeux anniversaire</b> à :\n' + } + newUsers.sort((use1, use2) => use1.promo - use2.promo); + newUsers.forEach((user, index, array) => { + if (index !== array.length - 1) { + msg += `├ <a href = "https://linkcs.fr/user/${user.login}">${user.name}</a>\n` + } else { + msg += `└ <a href = "https://linkcs.fr/user/${user.login}">${user.name}</a>\n` + } + }) + return bot.sendMessage(chatId, msg, { parse_mode: 'HTML' }); }) }); @@ -137,7 +155,9 @@ bot.onText(new RegExp(`/search (.+)|/search@${config.telegram.name} (.+)`), (msg return searchGroups(chan, research) }).then(groups => { if (groups.length == 0) return bot.sendMessage(chatId, 'Pas de groupe à ce nom...'); - var resp = 'Voici les différentes associations. Faites /add XXXX pour ajouter les anniversaires de ses membres. '; + const aleatgroup = groups[Math.floor(Math.random() * groups.length)]; + const aleatcompo = aleatgroup.compositions[Math.floor(Math.random() * aleatgroup.compositions.length)]; + var resp = `Voici les différentes associations et leurs compositions. Pour ajouter les membres d'une composition à votre liste d'anniversaire, faites par exemple /add ${aleatcompo.id} pour ajouter les membres de la composition ${aleatcompo.label} de l'asso ${aleatgroup.name}. `; // LinkCS limite à 20 résultats if (groups.length == 20) resp += 'La recherche est limitée à 20 choix.' resp += '\n\n' @@ -238,11 +258,10 @@ bot.onText(/\/info/, msg => { if (chan.token.length === 0) return bot.sendMessage(chatId, `Une demande de connexion a été faite par @${chan.username} mais n'a toujours pas été acceptée.\n${config.website.protocol}://${config.website.hostname}/?state=${chan.state}`); return Promise.all([ getMe(chan), - getCompos(chan.chatId), getCompos(chan.chatId).then(groups => { return Promise.all(groups.map(group => getCompoGroupById(chan, group))) }) - ]).then(([me, groups, cgs]) => { + ]).then(([me, cgs]) => { groups = {} cgs.forEach(cg => { if (!groups[cg.group.id]) { @@ -264,7 +283,7 @@ bot.onText(/\/info/, msg => { } // affiche la liste des associations à souhait - if (groups.length === 0) { + if (cgs.length === 0) { msg += 'La liste des compositions est vide.\n'; msg += 'Vous pouvez en chercher via /search sonNom, puis faire un /add sonID\n' } else { diff --git a/website.js b/website.js index 375ccd4be5c756795316f305bf5400f1f571044d..a0455ee867dd84a278d14ff82df847370e3957d3 100644 --- a/website.js +++ b/website.js @@ -48,7 +48,7 @@ app.get('/auth', function (req, res) { if (!req.query.code || !req.query.state) return res.sendFile(`${__dirname}/auth.html`) return getFirstToken(req.query.code, req.query.state).then(chan => { - bot.sendMessage(chan.chatId, `@${chan.username} s'est connecté à OAuth2, shall we begin?`) + bot.sendMessage(chan.chatId, `@${chan.username} s'est connecté à OAuth2, shall we begin?\nFaites /search XXX pour chercher une composition sur linkCS et ajouter ses membres à votre liste d'anniversaire.`) res.redirect(301, `${config.website.protocol}://${config.website.hostname}/auth`) }) })