I've developed a telegram bot using Node.js and node-telegram-bot-api module that sends a message and an inline keyboard to the users, what I'm trying is to achieve that after the user clicks the button, the inline keyboard must disappear. I'm using editMessageReplyMarkup but it gives the mentioned errors
Reference: Method editMessageReplyMarkup removes inline keybord
Part of code:
bot.on('callback_query', function onCallbackQuery(example) {
const action = example.data
const msg_id = example.message.from.id
const chat_id = example.from.id
//console.log(example.from.id)
if (action == 'FM') {
bot.editMessageReplyMarkup({
reply_markup: {
inline_keyboard: [
[
],
]
}
}, {
chat_id: chat_id,
message_id: msg_id
});
}
});
Error:
Unhandled rejection Error: ETELEGRAM: 400 Bad Request: message to edit not found
I've tried the following solution as well but it doesn't work
Reference:
How hide or delete inline button after click?
bot.on('callback_query', function onCallbackQuery(example) {
const action = example.data
const msg_id = example.message.from.id
const chat_id = example.from.id
console.log(example.from.id)
if (action == 'FM') {
console.log(action)
console.log("FM")
console.log(msg_id)
// console.log(example.message.message_id)
bot.editMessageReplyMarkup({
chat_id: chat_id,
message_id: msg_id,
reply_markup: JSON.stringify({
keyboard: []
})
}
);
}
});
Error:
Unhandled rejection Error: ETELEGRAM: 400 Bad Request: message identifier is not specified
You're referring to wrong msg_id.
It should be
const msg_id = example.message.message_id
bot.editMessageReplyMarkup({
reply_markup: {
inline_keyboard: [
[
],
]
}
}, {
chat_id: chat_id,
message_id: msg_id
});
execute console.log(example) to get a clear idea of the response
Use this order parameters
bot.telegram.editMessageReplyMarkup(chatId, messageId, newMarkup);
Related
I am new trying to validate the incoming data i.e. request into my rest API and if correct then passing the control of the execution to controller folder where other logic executes. My request validates successfully and throws the error if req is incorrect in the format -> "error": "\"fname\" must only contain alpha-numeric characters".
But I want it in this format ->
"errors": [
{
"field": "firstname"
"msg": "Bad Request..whatever i want here"
"type": "Invalid"
}
]
This is my code -
const joi = require('#hapi/joi');
const sschema = joi.object({
servicename: joi.string().min(6).max(9).required(),
firstname: joi.string().min(4).max(255).required(),
lastname: joi.string().min(4).max(255).required(),
address: joi.string().min(20).max(256).required(),
countrycode: joi.string().length(2).valid('ca', 'us', 'uk', 'in').required()
});
const parametersValidation = async (req, res, next) => {
try {
await sschema.validateAsync(req.body, { abortEarly: true });
} catch (error) {
let var1 = error.details[0].message;
return res.status(400).send({
errors: [
{
type: 'Invalid',
field: var1,
message: 'Bad Request',
},
],
});
}
next(); // next will control the transfer to controller if all things are correct.
};
P.S. - I am using hapi/joi - 17.1.1 AND .messages() method is not working either I tried.
Hey Guys I was making an telegram bot using nodejs telegram bot api when I cam across a problem .
I wanted to dispaly a html parsed message and an inline keyboard below it
bot.onText(/\/help/, async (msg) => {
help_msg = `<b>This is the link for the advanced help</b>`;
var adv_help = {
reply_markup: JSON.stringify({
inline_keyboard: [
[
{
text: "Advanced Help",
url: "https://telegra.ph/Advanced-Help-for-Cloud-Torrenter-07-31",
},
],
],
}),
};
bot.sendMessage(msg.chat.id, help_msg, { parse_mode: "HTML" }, adv_help);
});
But in this case the inline key board button wont appear at all only the parsed help_msg appears
Picture Here
But if change the last line to
bot.sendMessage(msg.chat.id, help_msg, adv_help ,{ parse_mode: "HTML" });
The unparsed help_msg that is the raw help_msg with appears with the inline keyboard
Picture here
can anyone suggest a method to get both the parsed text and button together
I finaly got the answer
the command sintax is: bot.sendMessage(chatid, message, options)
object "reply_markup", stays within the options
like
const adv_help = {
reply_markup: {
inline_keyboard: [
[
{
text: "Advanced Help",
url: "https://telegra.ph/Advanced-Help-for-Cloud-Torrenter-07-31",
}
],
],
},
parse_mode: 'HTML'
}
bot.sendMessage(msg.chat.id, `<b>This is the link for the advanced help</b>`, adv_help)
Im trying to create a classroom using googles classroom API. Whenever run the classroom.create function I always receive the same error message. I'm using the JSON format from their docs but I just can't get it to work. I think I must be missing something.
This is the function:
async function listCourses(auth) {
const classroom = google.classroom({ version: 'v1', auth });
//Read data from JSON
let data = fs.readFileSync("class.json");
let course = JSON.parse(data);
//Try and create course
try {
const res = await classroom.courses.create(course);
console.log(res.data);
}
catch (error) {
console.log(error)
}
//List all current courses
classroom.courses.list({
pageSize: 10,
}, (err, res) => {
if (err) return console.error('The API returned an error: ' + err);
const courses = res.data.courses;
if (courses && courses.length) {
console.log('Courses:');
courses.forEach((course) => {
console.log(`${course.name} (${course.id})`);
});
} else {
console.log('No courses found.');
}
});
}
This is the JSON:
{
"id": "157942918368",
"name": "English - 9Y",
"section": "Period 2",
"ownerId": "me",
"courseState": "ACTIVE"
}
This is the error message:
code: 400,
errors: [
{
message: `Invalid JSON payload received. Unknown name "name": Cannot bind query parameter. Field 'name' could not be found in request message.\n` +
`Invalid JSON payload received. Unknown name "ownerId": Cannot bind query parameter. Field 'ownerId' could not be found in request message.\n` +
`Invalid JSON payload received. Unknown name "courseState": Cannot bind query parameter. Field 'courseState' could not be found in request message.\n` +
`Invalid JSON payload received. Unknown name "id": Cannot bind query parameter. Field 'id' could not be found in request message.\n` +
`Invalid JSON payload received. Unknown name "section": Cannot bind query parameter. Field 'section' could not be found in request message.`,
reason: 'invalid'
}
]
I believe your goal as follows.
You want to create new course using googleapis for Node.js.
Modification points:
At googleapis for Node.js, please put the request body to resource and/or requestBody.
I think that the reason of your error message is due to this.
When "id": "157942918368", is used, an error of Request contains an invalid argument. occurs.
When "courseState": "ACTIVE" is used, an error of "#CourseStateDenied This user cannot create or transition courses into the requested state." occurs.
"PROVISIONED" can be used in this case.
When above points are reflected to your script, it becomes as follows.
Modified script:
From:
const res = await classroom.courses.create(course);
To:
const res = await classroom.courses.create({ requestBody: course });
or
const res = await classroom.courses.create({ resource: course });
And also, please modify your request body as follows.
From:
{
"id": "157942918368",
"name": "English - 9Y",
"section": "Period 2",
"ownerId": "me",
"courseState": "ACTIVE"
}
To:
{
"name": "English - 9Y",
"section": "Period 2",
"ownerId": "me",
}
Note:
In this modification, it supposes that your const classroom = google.classroom({ version: 'v1', auth }); can be used for using the method of courses.create in Classroom API.
References:
Method: courses.create
googleapis for Node.js
I am using version 7.2.0 of firebase admin to send fcm push notification, using sendMutlicast method:
async function sendPushRequest({tokens, title, body, customData}) => {
const message = {
notification: {
title,
body,
},
data: customData,
tokens,
}
return firebase.messaging().sendMulticast(message)
}
This is the error I am getting
Error: Exactly one of topic, token or condition is required
at FirebaseMessagingError.Error (native)
at FirebaseMessagingError.FirebaseError [as constructor] (/user_code/node_modules/firebase-admin/lib/utils/error.js:39:28)
...
I tried logging the data and here is the object that sendPushRequest function is called with:
{
tokens: [ null, null, null, 'home-test', null, null ], // this one is a recent sample, I've been getting this error for a while now
title: 'some string',
body: 'some other string',
customData: {
title: 'some string',
body: 'some other string',
bigText: 'again another string',
color: '#9f0e27',
smallIcon: 'notificon',
sound: 'default'
}
}
I'm not sure what is causing the error!
I struggled with this problem too, its quite difficult to configure google admin firebase in nodejs. I find out there is a package that can handle this nicely.
https://www.npmjs.com/package/fcm-notification
but it has some little problem . you can not pass it multiple firebase configuration. here is some example :
const fcm = require('fcm-notification');
const fcm_key = require('../config/customer/fcm.json');
const FcM = new fcm(fcm_key);
module.exports.sendToSingleUser = async (message, token) => {
let message_body = {
notification: {
...message
},
token: token
};
FcM.send(message_body, function (err, response) {
if (err) {
return err
} else {
return response
}
})
}
Facing this error too. Figure out that our tokens array contains null or undefiend value. Resolved by remove that from tokens array and everything works fine.
I have a node application which is interacting with IBM watson assistant.
I need to update the response output of a dialog node and I'm using the following watson api
var params = {
workspace_id: //,
dialog_node: 'greeting',
new_dialog_node: 'greeting',
new_output: {
text: 'Hello! What can I do for you?'
//add more than one text
}
};
assistant.updateDialogNode(params, function(err, response) {
if (err) {
console.error(err);
} else {
console.log(JSON.stringify(response, null, 2));
}
});
the API only accepts object type text:'Hello! What can I do for you?' this also overwrites the previous response
the error [ { message: 'output should be of type Object', path: '.output' } ]
How can I update the Dialog and add multiple responses at the same time or update the existing one?
thanks in advance!
Have you tried the following format for new_output?
{
"text": {
"values": [
"first response",
"second response"
],
"selection_policy": "sequential"
}