Skip to content

Commit

Permalink
Close #26
Browse files Browse the repository at this point in the history
  • Loading branch information
Sylvain METAYER committed Mar 14, 2018
1 parent aceb927 commit 69fe517
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
8 changes: 8 additions & 0 deletions helper/chatsHandler.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
let chats = undefined;

let { state } = require("./variables");

module.exports.init = (_chats) => {
chats = _chats;
}
Expand Down Expand Up @@ -66,4 +68,10 @@ module.exports.pushItem = (id, key, value) => {
*/
module.exports.getAttr = (id, key) => {
return chats[id][key];
}

module.exports.reset = (id) => {
chats[id] = {
current_state: state.none
}
}
5 changes: 3 additions & 2 deletions helper/variables.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@ module.exports.state = {
};

module.exports.regex = {
start: /^\/start$/i,
start: /^\/?start$/i,
parrot: new RegExp(`${process.env.TOKEN}say (.*)`),
hello: new RegExp(`${process.env.TOKEN}hello`),
identity: new RegExp(`${process.env.TOKEN}identity`),
dev_question: new RegExp(`${process.env.TOKEN}devQuestion`),
network_question: new RegExp(`${process.env.TOKEN}networkQuestion`),
post_question: new RegExp(`${process.env.TOKEN}postQuestion`)
post_question: new RegExp(`${process.env.TOKEN}postQuestion`),
reset: /^(?:recommencer)|(?:Je veux recommencer)|(?:Je souhaite recommencer)$/i
};

module.exports.config = {
Expand Down
6 changes: 5 additions & 1 deletion partials/common.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
let { getCurrentState, setCurrentState } = require("../helper/chatsHandler");
let { getCurrentState, setCurrentState, reset } = require("../helper/chatsHandler");

const state = require("../helper/variables").state;
const regex = require("../helper/variables").regex;
Expand All @@ -19,6 +19,10 @@ module.exports.run = function (msg) {
let replay = [];

switch (true) {
case regex.reset.test(msg.text):
bot.sendMessage(id, "Recommençons ! Tapez 'start' pour commencer")
reset(id);
break;
case regex.start.test(msg.text) && getCurrentState(id) == state.none:
bot.sendMessage(id, "Bonjour !\nJe me présente, je suis un petit bot de recrutement.\nSi vous le souhaitez, je vais vous poser quelques questions afin de voir quel poste pourrait vous convenir. Êtes-vous prêt ?", {
"reply_markup": {
Expand Down
28 changes: 27 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const TelegramBot = require('node-telegram-bot-api');
const Bot = require("./bot");
const messageHelper = require("./helper/test_message_helper");

describe("Simple test", function () {
describe("My Chat Bot Tests", function () {

let client, server, token, telegramBot, testBot;

Expand Down Expand Up @@ -231,6 +231,32 @@ describe("Simple test", function () {
// TODO Write test
this.skip();
});

it("Should reset the conversation", function () {
this.slow(1000);
this.timeout(3000);

let expected = {
current_state: messageHelper.getStates().none
}

return messageHelper.assert("recommencer", "Recommençons ! Tapez 'start' pour commencer")
.then(() => expect(messageHelper.getClientChatData(testBot)).deep.equal(expected))
});

it("Should reset the conversation at any time.", function () {
this.slow(2000);
this.timeout(3000);

let expected = {
current_state: messageHelper.getStates().none
}

return messageHelper.assert("/start", "Bonjour !\nJe me présente, je suis un petit bot de recrutement.\nSi vous le souhaitez, je vais vous poser quelques questions afin de voir quel poste pourrait vous convenir. Êtes-vous prêt ?")
.then(() => messageHelper.assert("oui", ["Parfait, commençons !", "Voici une question de développement, êtes-vous prêt ? (oui/non)"]))
.then(() => messageHelper.assert("Je veux recommencer", "Recommençons ! Tapez 'start' pour commencer"))
.then(() => expect(messageHelper.getClientChatData(testBot)).deep.equal(expected))
});
})


Expand Down

0 comments on commit 69fe517

Please sign in to comment.