Skip to content

Commit

Permalink
[Controllers] /start, /auth, /add, /list, /help
Browse files Browse the repository at this point in the history
  • Loading branch information
MilosD authored and MilosD committed May 30, 2018
1 parent 1036f5b commit 138127b
Show file tree
Hide file tree
Showing 6 changed files with 285 additions and 25 deletions.
103 changes: 103 additions & 0 deletions controllerts/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
'use strict'

const Telegram = require('telegram-node-bot')
const TelegramBaseController = Telegram.TelegramBaseController

// var auth = {};

class AuthController extends TelegramBaseController {
constructor(tg) {
super();
this.tg = tg;
this.auth = {};
}

/**
* @param {Scope} $
*/
// const

authHandler($) {
const userId = $.chatId;
this.tg.api.sendMessage(userId, 'Hi')

if (this.checkIsAuth(userId)) {
this.tg.api.sendMessage(userId,`You are already auth ${this.auth[userId].email}.`)
return;
}


const form = {
email: {
q: 'Send me your email',
error: 'Sorry, wrong email format',
validator: (message, callback) => {
if(isEmailValid(message.text)) {
callback(true, message.text) //you must pass the result also
return
}

callback(false)
}
},
password: {
q: 'Send me password',
error: 'sorry, wrong input',
validator: (message, callback) => {
if(message.text) {
callback(true,message.text)
return
}

callback(false)
}
}
}

$.runForm(form, (result) => {
console.log(result);
this.auth[userId] = result;

$.sendMessage(`Thank you for log in ${result.email}.\n You can now continue using 64idea`);
console.log(this.auth);
})


// if (auth['']) {

// }


}


checkIsAuth(userId){

if (userId in this.auth) {
// if (this.auth[userId].username !== undefined) {
// return true;
// }
return true;

} else {
return false;
}
}


get routes() {
return {
'authCommand': 'authHandler',
}
}



}

function isEmailValid(email) {
var regex = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return regex.test(email);
}

module.exports = AuthController;
24 changes: 24 additions & 0 deletions controllerts/help.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict'

const Telegram = require('telegram-node-bot')
const TelegramBaseController = Telegram.TelegramBaseController


class HelpController extends TelegramBaseController {
/**
* @param {Scope} $
*/
helpHandler($) {
let response = 'Welcome to 64idea.com help section.\n\nPut */* in chat area to see available commands.\n\nIf you need any help go to our site www.64idea.com/help for further assistance.'
$.sendMessage(response,{ parse_mode: 'Markdown' });
}


get routes() {
return {
'helpCommand': 'helpHandler',
}
}
}

module.exports = HelpController;
119 changes: 119 additions & 0 deletions controllerts/idea.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
'use strict'

const Telegram = require('telegram-node-bot')
const TelegramBaseController = Telegram.TelegramBaseController


class IdeaController extends TelegramBaseController {

constructor(authController){
super();
this.authController = authController;
}

/**
* @param {Scope} $
*/
addHandler($) {

const userId = $.chatId;

if (!this.isAuth(userId)) {
$.sendMessage('Sorry, pls auth first at /auth');
return;
}

let idea = $.message.text.split(' ').slice(1).join(' ');

if (!idea) {
return $.sendMessage('Sorry, pls insert some idea');
}

$.getUserSession('ideas')
.then( ideas => {
if (!Array.isArray(ideas)) {
$.setUserSession('ideas',[idea])
} else {
$.setUserSession('ideas' , ideas.concat([idea]));
console.log(idea);
}
$.sendMessage('Added new idea');
});
}

listHandler($){

// const ideas = $.getUserSession('ideas');

// if (!ideas) {
// $.sendMessage( this.getFormatedIdeas(ideas), { parse_mode: 'Markdown' });
// } else {
// $.sendMessage('..such.. empty.. ideas..');
// }
const userId = $.chatId;

if (!this.isAuth(userId)) {
$.sendMessage('Sorry, pls auth first at /auth');
return;
}


$.getUserSession('ideas').then( ideas => {

if(isEmpty(ideas)){
$.sendMessage('such.. empty.. list..');
}else{
$.sendMessage( this.getFormatedIdeas(ideas), { parse_mode: 'Markdown' });
}

});


}

get routes() {
return {
'addCommand': 'addHandler',
'listCommand': 'listHandler'
}
}

getFormatedIdeas(ideasList){
let response = '*Your ideas:*\n\n';

// if (!ideasList) {

// }

ideasList.forEach(element => {
response += `- ${element}\n`;
});
return response;
}

isAuth(userId){
console.log('testing auth');
console.log(`userId: ${userId}`);
// console.log(`auth obj: ${this.auth}`);

if (!(userId in this.authController.auth)) {
return false;
}else{
return true;
}
}


}

function isEmpty(obj) {
for(var prop in obj) {
if(obj.hasOwnProperty(prop))
return false;
}

return JSON.stringify(obj) === JSON.stringify({});
}


module.exports = IdeaController;
22 changes: 0 additions & 22 deletions controllerts/ping.js

This file was deleted.

23 changes: 23 additions & 0 deletions controllerts/start.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict'

const Telegram = require('telegram-node-bot')
const TelegramBaseController = Telegram.TelegramBaseController


class StartController extends TelegramBaseController {
/**
* @param {Scope} $
*/
startHandler($) {
$.sendMessage(`Hi there.\nPut / in chat to start`,{ parse_mode: 'Markdown' });
}


get routes() {
return {
'startCommand': 'startHandler',
}
}
}

module.exports = StartController;
19 changes: 16 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,22 @@ const Telegram = require('telegram-node-bot')
const TextCommand = Telegram.TextCommand
const tg = new Telegram.Telegram('618842473:AAG3b8qTzX28w2xFfCF2kI_Yeh-8aZfslf4',{ workers : 1 });

const PingController = require('./controllerts/ping');
const OtherwiseController = require('./controllerts/otherwise');
let IdeaController = require('./controllerts/idea');
let OtherwiseController = require('./controllerts/otherwise');
let AuthController = require('./controllerts/auth');
let HelpController = require('./controllerts/help');
let StartController = require('./controllerts/start');

let authController = new AuthController(tg);
let ideaController = new IdeaController(authController);
let helpController = new HelpController();
let startController = new StartController();


tg.router
.when(new TextCommand('/ping', 'pingCommand'),new PingController())
.when(new TextCommand('/start', 'startCommand'),startController)
.when(new TextCommand('/add', 'addCommand'),ideaController)
.when(new TextCommand('/list', 'listCommand'),ideaController)
.when(new TextCommand('/auth', 'authCommand'),authController)
.when(new TextCommand('/help', 'helpCommand'),helpController)
.otherwise(new OtherwiseController());

0 comments on commit 138127b

Please sign in to comment.