Skip to content

Commit

Permalink
Merge branch 'try_fix_10' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvainmetayer committed Mar 26, 2018
2 parents cdeb7f4 + 86aa814 commit d4236f0
Show file tree
Hide file tree
Showing 21 changed files with 814 additions and 576 deletions.
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
"description": "EPSI School Project - Telegram Bot",
"main": "src/index.js",
"scripts": {
"test": "NODE_ENV=test node ./node_modules/.bin/mocha --use_strict --exit",
"test": "NODE_ENV=test node ./node_modules/.bin/mocha ./tests --use_strict --exit",
"start": "NODE_ENV=development node src/index.js",
"forever": "NODE_ENV=production forever start src/index.js",
"coverage": "./node_modules/.bin/istanbul cover ./node_modules/mocha/bin/_mocha --use_strict --exit --report lcovonly -- -R spec && ./node_modules/.bin/codecov",
"coverage": "./node_modules/.bin/istanbul cover ./node_modules/mocha/bin/_mocha ./tests --use_strict --exit --report lcovonly -- -R spec && ./node_modules/.bin/codecov",
"lint": "./node_modules/.bin/standard",
"lint-fix": "./node_modules/.bin/standard --fix"
"lint-fix": "./node_modules/.bin/standard --fix",
"single-test": "NODE_ENV=test node ./node_modules/.bin/mocha $NAME --use_strict --exit",
"all-single-test": "for file in ./tests/*; do NAME=$file npm run single-test; if test $? -ne 0; then exit $?; fi; done"
},
"pre-commit": [
"lint"
Expand Down Expand Up @@ -48,8 +50,10 @@
"homepage": "https://github.com/sylvainmetayer/Telegram_Bot#readme",
"dependencies": {
"airbrake-js": "^1.0.7",
"bluebird": "^3.5.1",
"dotenv": "^5.0.1",
"node-telegram-bot-api": "^0.30.0",
"nodemailer": "^4.6.3",
"npm": "^5.7.1",
"vm2": "^3.5.2"
},
Expand Down
80 changes: 64 additions & 16 deletions src/bot.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
'use strict'

const TelegramBot = require('node-telegram-bot-api')
const chatHandler = require('./helper/chatsHandler')

const { state } = require('./helper/variables')

let { getCurrentState } = require('./helper/chatsHandler')

let identitySwitch = require('./partials/identity')
let devQuestions = require('./partials/dev_question')
let common = require('./partials/common')
let networkQuestions = require('./partials/network_question')
let postQuestions = require('./partials/post_questions')
const Promise = require('bluebird')

const partials = [common, identitySwitch, devQuestions, networkQuestions, postQuestions]

Expand All @@ -21,8 +19,6 @@ class MyChatBot extends TelegramBot {

this.chats = {}

chatHandler.init(this.chats)

partials.forEach((partial) => {
partial.init(this)
})
Expand All @@ -35,47 +31,99 @@ class MyChatBot extends TelegramBot {
let chatId = msg.from.id
let trigger = false

if (process.env.NODE_ENV === 'development') { console.log('\x1b[1;104m', msg.text, '\x1b[0m') }

if (!(chatId in this.chats)) {
this.chats[chatId] = {
current_state: state.none
}
this.reset(chatId)
}

let replays = []
let replay = []
partials.forEach((partial) => {
replay = []
if (!trigger) { [trigger, replay] = partial.run(msg) }
if (!trigger) { [trigger, replay, this.chats] = partial.run(msg, this.chats) }
replays.push.apply(replays, replay)
})

replays.forEach((partial) => {
[replay, replay] = partial.run(msg)
[replay, replay, this.chats] = partial.run(msg, this.chats)
})

if (!trigger) {
this.sendMessage(chatId, "Je n'ai pas compris votre demande.")
this.stackMessage(chatId, "Je n'ai pas compris votre demande.")
console.log(msg)
console.log(getCurrentState(chatId))
console.log(this.chats[chatId].current_state)
}

this.flush(chatId)
})

this.on('polling_error', (error) => {
console.log(error)
throw Error('Polling error')
throw Error(error)
})
}

stop () {
this.stopPolling()
}

sendMessage (chatId, text, options = {
reset (chatId) {
this.chats[chatId] = {
current_state: state.none,
queue: [],
devQuestionCount: 0,
networkQuestionCount: 0,
currentQuestion: undefined,
currentQuestionNetwork: undefined,
scoreDev: 0,
scoreNetwork: 0,
answeredQuestions: [],
answeredNetworkQuestions: []
}
}

stackMessage (chatId, text, options = {
'reply_markup': {
hide_keyboard: true
}
}) {
super.sendMessage(chatId, text, options)
this.chats[chatId].queue.push({chatId: chatId, text: text, options: options})
}

flush (chatId) {
let that = this
let queue = this.chats[`${chatId}`].queue

if (queue === undefined) {
console.log(this.chats[`${chatId}`])
throw Error('Queue is undefined !')
}

if (queue === []) {
throw Error('Queue cannot be empty !')
}

// Trust me, I'm an engineer. Also, I'm sorry.
if (process.env.NODE_ENV === 'test') {
queue.forEach((element) => {
if (process.env.NODE_ENV === 'development') { console.log('\x1b[1;45m', element.text, '\x1b[0m') }
return that.sendMessage(element.chatId, element.text, element.options)
.catch((error) => {
throw error
})
})
this.chats[`${chatId}`].queue = []
} else {
return Promise.mapSeries(queue, (element) => {
if (process.env.NODE_ENV === 'development') { console.log('\x1b[1;45m', element.text, '\x1b[0m') }
return that.sendMessage(element.chatId, element.text, element.options)
.catch((error) => {
throw error
})
}).then(() => {
this.chats[`${chatId}`].queue = []
})
}
}
}

Expand Down
15 changes: 15 additions & 0 deletions src/helper/airbrake.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const AirbrakeClient = require('airbrake-js')

module.exports.run = () => {
if (process.env.PROJECT_KEY !== undefined && process.env.PROJECT_ID !== undefined) {
var airbrake = new AirbrakeClient({
projectId: process.env.PROJECT_ID,
projectKey: process.env.PROJECT_KEY
})

airbrake.addFilter(function (notice) {
notice.context.environment = process.env.NODE_ENV
return notice
})
}
}
77 changes: 0 additions & 77 deletions src/helper/chatsHandler.js

This file was deleted.

Loading

0 comments on commit d4236f0

Please sign in to comment.