How to create pagination with inline keyboard in telegram - node.js

I am creating a Telegram bot wit Node.js and I am using node-telegram-bot-api module.
My current issue is:
To create pagination with inline keyboard.
In documentation here, has an interesting example of what I need.
For appearances, I must use method editMessageText but for update inline keyboard I need to transfer param inline_message_id. Unfortunately I could not understand how to do it.
I will be very much appreciate for any example to update inline keyboard and how it release in this example.

You need pass updated pagination with editMessageText:
var bookPages = 100;
function getPagination( current, maxpage ) {
var keys = [];
if (current>1) keys.push({ text: `«1`, callback_data: '1' });
if (current>2) keys.push({ text: `‹${current-1}`, callback_data: (current-1).toString() });
keys.push({ text: `-${current}-`, callback_data: current.toString() });
if (current<maxpage-1) keys.push({ text: `${current+1}›`, callback_data: (current+1).toString() })
if (current<maxpage) keys.push({ text: `${maxpage}»`, callback_data: maxpage.toString() });
return {
reply_markup: JSON.stringify({
inline_keyboard: [ keys ]
})
};
}
bot.onText(/\/book/, function(msg) {
bot.sendMessage(msg.chat.id, 'Page: 25', getPagination(25,bookPages));
});
bot.on('callback_query', function (message) {
var msg = message.message;
var editOptions = Object.assign({}, getPagination(parseInt(message.data), bookPages), { chat_id: msg.chat.id, message_id: msg.message_id});
bot.editMessageText('Page: ' + message.data, editOptions);
});

Related

Buttons and bold text in bot.sendMessage

help me understand what the problem is.
Why are the buttons (menuOptions) not displayed?
if (text === '/start') {
ust[chatId] = 0;
bot.sendMessage(chatId, `${msg.from.first_name}, <b>Добро пожаловать</b> ✌️\n\nДоступные возможности:`, {
parse_mode: "HTML",
menuOptions
})
}
Code menuOptions:
module.exports = {
menuOptions: {
reply_markup: JSON.stringify({
inline_keyboard: [
[{text: 'Списки', callback_data: '/list'}],
[{text: 'Частые вопросы', callback_data: '/quests'}, {text: 'Распространенные проблемы', callback_data: '/problems'}]
]
})
}
node-telegram-bot-api on Node JS
I tried many ways, remade the menuOptions structure, as soon as I did not insert it.
Your code specifies menuOptions key-value pair in the options parameter of the sendMessage method. What you need to do is directly use reply_markup key.
const { menuOptions } = require('./path/to/menu_options');
if (text === '/start') {
ust[chatId] = 0;
bot.sendMessage(chatId, `${msg.from.first_name}, <b>Добро пожаловать</b> ✌️\n\nДоступные возможности:`, {
parse_mode: "HTML",
reply_markup: menuOptions.reply_markup
})
}
If you want to use other properties than just reply_markup in the menuOptions object, javascript spread operator might help:
{
parse_mode: 'HTML',
...menuOptions
}

Update Dialogflow "Transfer Call" field from backend ( Node.js )

I try to update phone number in "Transfer Call" field under "Responses" tab ("TELEPHONY" -> "ADD RESPONSES" button ) for given intent using Node.js but I cannot.
New update removes old "Transfer Call" field with the old phone number (which I created by hand in console for testing purposes)
Please help
Here is example code:
const dialogflow = require('dialogflow')
const intentsClient = new dialogflow.IntentsClient({ keyFilename: 'key.json' })
const fulfillmentMessages = [ { platform: 'TELEPHONY',
telephonySynthesizeSpeech: { text: 'Hello World' } },
{ platform: 'TELEPHONY',
telephonyTransferCall: { phoneNumber: '+12132954242' } },
{ text: { text: [ '' ] } } ]
const intent = {
name: 'projects/example/agent/intents/2ef3e0b6-6cd7-4d5b-a8ca-ce11111125e019',
displayName: 'Test',
fulfillmentMessages: fulfillmentMessages
}
const updateIntentRequest = { intent: intent }
intentsClient.updateIntent(updateIntentRequest).then((data) =>{ console.log(data)}, (e) => {
console.log(e) })
Detailed response can be found here however heres the correct code sample (tested and working)
const dialogflow = require('dialogflow');
const intentsClient = new dialogflow.v2beta1.IntentsClient({ keyFilename: 'key.json' }) //Note that dialogflow will be using v2beta1 api
const message_to_set = [
{
platform: 10,
telephonySynthesizeSpeech: {
text : 'Hello World'
},
telephonyTransferCall: {
phoneNumber: '+12132954242'
}
}
]
const intent = {
name: 'projects/example/agent/intents/2ef3e0b6-6cd7-4d5b-a8ca-ce11111125e019',
displayName: 'Forward',
messages: message_to_set //Note parameter was switched from fulfillmentMessages to messages
}
const updateIntentRequest = { intent: intent }
intentsClient.updateIntent(updateIntentRequest).then((data) =>{ console.log(data)}, (e) => { console.log(e) })

How to export and import functions for telegrams bot?

I'm create a bot telegram with two buttons. On each button I want to hang the action. I want to transfer these actions to another file. How can I do that?
const Telegraf = require("telegraf");
const session = require("telegraf/session");
const Composer = require('telegraf/composer');
const bot = new Telegraf('Token')
const first = require('./command/first');
bot.command('start', (ctx) => {
const markdown = `
Hi! Click on the button 1 or 2!`;
ctx.telegram.sendMessage(ctx.message.chat.id, markdown, {
parse_mode: 'Markdown',
reply_markup: {
keyboard: [
['1', '2'],
],
resize_keyboard: true
},
disable_notification: false
});
});
bot.use(session());
bot.use(Telegraf.log())
bot.on('1', first.hears()) ///myfunction command
bot.startPolling();
console.log(Telegraf.log());
and file ./command/first
module.exports = {
hears: function () {
console.log("debug 1");
bot.action('1', (ctx) => {
const markdown = ` Type some text...`;
ctx.telegram.sendMessage(ctx.message.chat.id, markdown, {
parse_mode: 'Markdown',
reply_markup: {
keyboard: [
['🔙 Back'],
],
resize_keyboard: true
},
disable_notification: false
});
})
}
};
but nothing works. When starting the bot writes immediately debug 1
And nothing.. Help me please!
Firstly Change:
bot.on('1', first.hears()) // on is for events
to
bot.hears('1', first.hears()) // hears listens for the specified string from bot
Then rewrite the module in /command/first to:
module.exports = {
hears: function (ctx) {
console.log("debug 1");
// Added markdown formatting
const message = `*Bold text* and _italic text_`;
ctx.telegram.sendMessage(ctx.message.chat.id, message, {
parse_mode: 'Markdown',
reply_markup: JSON.stringify({ // You also missed JSON.stringify()
keyboard: [
['🔙 Back'],
],
resize_keyboard: true
}),
disable_notification: false
});
}
}
This should work. I hope this helps.

How to create and send a backchannel event with every message in bot framework?

I'm trying to access a session variable from the botbuilder middleware in send hook:
bot.use({
botbuilder: function (session, next) {
session.send(); // it doesn't work without this..
session.sendTyping();
console.log('session.userData', session.userData['UI_Changes']);
var reply = createEvent("UI_Changes", session.userData['UI_Changes'], session.message.address);
session.send(reply);
// session.userData['UI_Changes'] = {};
next();
},
send: function (event, next) {
// console.log('session.userData', session.userData['UI_Changes']);
// var reply = createEvent("UI_Changes", session.userData['UI_Changes'], session.message.address);
// session.send(reply);
// session.userData['UI_Changes'] = {};
next();
}
});
But since session is not available in send, how can I access the userData?
createEvent simply creates a backchannel event:
//Creates a backchannel event
const createEvent = (eventName, value, address) => {
var msg = new builder.Message().address(address);
msg.data.type = "event";
msg.data.name = eventName;
msg.data.value = value;
return msg;
}
I found this answer on stackoverflow:
send: function (message, next) {
bot.loadSessionWithoutDispatching(message.address,function (error,session){
console.log(session.userData);
});
next();
}
But, when I try to create an event and send it, I'm not able to access the address
bot.loadSessionWithoutDispatching(event.address, function (error,session){
console.log('session not null?', session !== null ? "yes" : "no");
if(session !== null){
console.log('session.userData', session.userData['UI_Changes'], 'address:', session);
var reply = createEvent("UI_Changes", session.userData['UI_Changes'], event.address); //undefined
session.send(reply);
session.userData['UI_Changes'] = {};
}
});
both session.message and event.address are undefined inside the callback function. How can I possibly do a workaround?
event has following content:
event: { type: 'message',
text: 'You reached the Greeting intent. You said \'hi\'.',
locale: 'en-US',
localTimestamp: '2018-06-21T14:37:12.684Z',
from: { id: 'Steves#4MRN9VFFpAk', name: 'Steves' },
recipient: { id: 'pruthvi', name: 'pruthvi' },
inputHint: 'acceptingInput' }
whereas outside the loadSessionWithoutDispatching function it has:
event outside { type: 'message',
agent: 'botbuilder',
source: 'directline',
textLocale: 'en-US',
address:
{ id: 'A7nrBS4yINt2607QtKpxOP|0000048',
channelId: 'directline',
user: { id: 'pruthvi', name: 'pruthvi' },
conversation: { id: 'A7nrBS4yINt2607QtKpxOP' },
bot: { id: 'Steves#4MRN9VFFpAk', name: 'Steves' },
serviceUrl: 'https://directline.botframework.com/' },
text: 'You reached the Greeting intent. You said \'hi\'.' }
I've used bot.loadSession instead of loadSessionWithoutDispatching and it works fine.
I've used bot.loadSession instead of loadSessionWithoutDispatching and it works fine.

Chrome onMessage not working (?)

I've read about onMessage.addListener method in Chrome to pass some data from extensions to script. What I have now:
popup.js
window.onload = function(){
document.getElementById('searchButton').onclick = searchText;
};
function searchText(){
var search = document.getElementById('searchText').value; // f.ex "123"
if(search){
chrome.tabs.query({active:true,currentWindow:true},function(tabs){
chrome.tabs.executeScript(tabs[0].id,{file:search.js});
chrome.tabs.sendMessage(tabs[0].id,{method:'search',searchText:search});
});
}
}
search.js
chrome.runtime.onMessage.addListener(function(message,sender,sendResponse){
alert('text');
});
However, alert ('text') is never fired. What's the problem?
You should quote "search.js" and put the chrome.tabs.sendMessage call in the callback of chrome.tabs.executeScript:
function searchText(){
var search = document.getElementById('searchText').value; // f.ex "123"
if (search) {
chrome.tabs.query({active:true,currentWindow:true}, function(tabs) {
chrome.tabs.executeScript(tabs[0].id, {
file: 'search.js'
}, function() {
chrome.tabs.sendMessage(tabs[0].id, {
method: 'search',
searchText: search
});
});
});
}
}
If this suggestion does not help, inspect the popup and look for error messages.

Resources