-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Controllers] /start, /auth, /add, /list, /help
- Loading branch information
MilosD
authored and
MilosD
committed
May 30, 2018
1 parent
1036f5b
commit 138127b
Showing
6 changed files
with
285 additions
and
25 deletions.
There are no files selected for viewing
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
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; |
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
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; |
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
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; |
This file was deleted.
Oops, something went wrong.
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
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; |
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