Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New ctx pluging and askUser /cancel support #158

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions examples/plugin-ctx.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const TeleBot = require('../');

const bot = new TeleBot({
token: 'TELEGRAM_BOT_TOKEN',
usePlugins: ['ctx']
});

// On start command
bot.on('/start', msg => {

// Reply adding context
return msg.reply.text('Hi', {ctx: {
from: 'Hi',
info: 'You can pass whatever you want'
}});

});

// On any text message
bot.on('text', msg => {

// ctx exists
if (msg.ctx) {
return msg.reply.text('Context received 🎉');
}

return msg.reply.text('No context here 😖');

});

bot.start();
11 changes: 9 additions & 2 deletions plugins/askUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module.exports = {
console.error('ERROR using askUser plugin: you must specify at least one valid type. adding type \'text\'');
pluginConfig.messageTypes.push('text');
}

// On every message
bot.on(pluginConfig.messageTypes, (msg, props) => {

Expand All @@ -31,9 +32,15 @@ module.exports = {
// If no question, then it's a regular message
if (!ask) return;

// Delete user from list and send custom event
// Delete user from list
delete userList[id];
bot.event('ask.' + ask, msg, props);

// Send cancel event or custom event
if (msg.text === "/cancel") {
bot.event('ask.' + ask + '.cancel', msg, props);
} else {
bot.event('ask.' + ask, msg, props);
}

});

Expand Down
40 changes: 40 additions & 0 deletions plugins/ctx.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
Pass data to next user reply
*/


const userList = {};

module.exports = {
id: 'ctx',
defaultConfig: {},
plugin(bot) {

// On every message
bot.mod('message', (data) => {
let msg = data.message;

const id = msg.chat.id;
const ctx = userList[id];

// If no ctx, then return the message
if (!ctx) return data;

// If ctx deletit from the list and add it to the message
delete userList[id];
msg.ctx = ctx;
return data;
});

// Before call sendMessage method
bot.on('sendMessage', (args) => {
const id = args[0];
const opt = args[2] || {};

const ctx = opt.ctx;

if (ctx) userList[id] = ctx;
});

}
}