Created
November 13, 2018 18:43
-
-
Save 2gnc/30041083c2c91c0eb8118b96630f2055 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require('dotenv').config(); | |
const mongoose = require('mongoose'); | |
const Telegraf = require('telegraf'); | |
const Markup = require('telegraf/markup'); | |
const { | |
doIknowYou, | |
getUsersTeams, | |
getManagersTeams, | |
} = require('./utils/dbHelpers'); | |
const { | |
showBacklog, | |
showHelp, | |
showBirthdays, | |
showNextEvents, | |
} = require('./utils/botHelpers'); | |
const { | |
REPLIES, | |
COMMANDS, | |
} = require('./utils/phrases'); | |
const bot = new Telegraf(process.env.TOKEN); | |
mongoose.connect(process.env.MONGO, { useNewUrlParser: true }) | |
.then(() => { console.log('подключились к базе'); }) | |
.catch(() => console.log('не удалось подключиться')); | |
async function main(ctx, user) { | |
const userPlaysIn = await getUsersTeams(user); | |
const userManages = await getManagersTeams(user); | |
// TODO: сделать выбор команды, в которой дальше все будет происходить. | |
// Пока считаем, что один пользователь может играть и админить только одну команду. | |
const [TEAM_PLAYS] = userPlaysIn; | |
const [TEAM_MANAGES] = userManages; | |
const mainMenu = [ | |
[COMMANDS.ALARM], | |
[COMMANDS.ALL_GAMES, COMMANDS.BIRTHDAYS], | |
[COMMANDS.HELP], | |
]; | |
ctx.reply(REPLIES.MAIN_MENU, Markup | |
.keyboard(mainMenu) | |
.resize() | |
.extra()); | |
bot.command('backlog', reply => reply.replyWithHTML(showBacklog())); | |
bot.hears(COMMANDS.HELP, reply => reply.replyWithHTML(showHelp())); | |
bot.command('help', reply => reply.replyWithHTML(showHelp())); | |
bot.hears(COMMANDS.BIRTHDAYS, async reply => reply.replyWithHTML(await showBirthdays(TEAM_PLAYS))); | |
bot.command('birthday', async reply => reply.replyWithHTML(await showBirthdays(TEAM_PLAYS))); | |
bot.hears(COMMANDS.ALARM, async reply => reply.replyWithHTML(await showNextEvents(true))); | |
bot.command('where', async reply => reply.replyWithHTML(await showNextEvents(true))); | |
bot.hears(COMMANDS.ALL_GAMES, async reply => reply.replyWithHTML(await showNextEvents())); | |
bot.command('calendar', async reply => reply.replyWithHTML(await showNextEvents())); | |
} | |
async function whoAreYou(ctx) { | |
const tgUsername = ctx.update.message.from.username; | |
if (!tgUsername) { | |
ctx.reply(REPLIES.NO_USERNAME); | |
return; | |
} | |
const user = await doIknowYou(ctx.update.message.from.username, true); | |
if (!user) { | |
ctx.reply(REPLIES.UNKNOWN_USER); | |
} else { | |
main(ctx, user); | |
} | |
} | |
bot.start(ctx => whoAreYou(ctx)); | |
bot.startPolling(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment