When I use getChat method get this error:
Unhandled rejection Error: ETELEGRAM: 400 Bad Request: chat_id is empty
at request.then.resp (/root/Documents/saveSampad/SaveSampadBot/node_modules/node-telegram-bot-api/src/telegram.js:280:15)
at tryCatcher (/root/Documents/saveSampad/SaveSampadBot/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/root/Documents/saveSampad/SaveSampadBot/node_modules/bluebird/js/release/promise.js:512:31)
at Promise._settlePromise (/root/Documents/saveSampad/SaveSampadBot/node_modules/bluebird/js/release/promise.js:569:18)
at Promise._settlePromise0 (/root/Documents/saveSampad/SaveSampadBot/node_modules/bluebird/js/release/promise.js:614:10)
at Promise._settlePromises (/root/Documents/saveSampad/SaveSampadBot/node_modules/bluebird/js/release/promise.js:693:18)
at Async._drainQueue (/root/Documents/saveSampad/SaveSampadBot/node_modules/bluebird/js/release/async.js:133:16)
at Async._drainQueues (/root/Documents/saveSampad/SaveSampadBot/node_modules/bluebird/js/release/async.js:143:10)
at Immediate.Async.drainQueues (/root/Documents/saveSampad/SaveSampadBot/node_modules/bluebird/js/release/async.js:17:14)
at runCallback (timers.js:781:20)
at tryOnImmediate (timers.js:743:5)
at processImmediate [as _immediateCallback] (timers.js:714:5)
Remember this syntax:
bot.sendMessage(CHAT_ID_HERE, "Message);
So, can you write here your part of code?
I try send img in chanel
get chat id => https://api.telegram.org/bot/getUpdates => _"chat":{"id":-1001382271711,_
use code
var chatId = -1001382271711;
a.bot.sendDocument(chatId, 'img.png');
And see erorr => _400 Bad Request: wrong file identifier/HTTP URL specified_
Where I do mistake?
@tyt34 Maybe you must specify full file path, otherwise use file_id (best choice).
File_id can be retrieved catching message.document.file_id (or message.photo.file_id) on bot.on('message', ...).
Then sendDocument(chatId, file_id).
Anyway try to use as possibile sendPhoto or sendSticker, etc. sendDocument only if you don't know type of file are you sending.
@sidelux Hi. I use this api to get chat information
bot.getChat(chatId, function(msg) {
console.log(msg.chat.username)
})
@aliir74 I think you should use this method.
bot.on('message', function (msg) {
console.log(msg.from.username);
});
Next save chat_id in database or wherever you like then (in a second moment):
bot.getChat(savedChatId, function(msg) {
console.log(msg.from.username);
});
@sidelux I saved chatId before in db and now read from db. but above error occur.
my entire function code is:
const bot = new TelegramBot(token, {polling: true});
userModel.find({}, function (err, users) {
if(!users) {
return
}
for(let i = 0; i < users.length; i++) {
let chatId = users[i].chatId
console.log(i, users.length)
bot.getChat(chatId, function(msg) {
console.log(i, "done")
users[i].username = msg.chat.username
users[i].save()
})
}
})
@aliir74 Try to print you chat_id in console.log before use getChat, 99% is empty or there is a type error.
try this solution before and chatId was'nt empty. how to fix type error problem?
chat_id must be a String, declare chatId = "" before loop or use toString() when assign, maybe js parse it to a number and if it starts with 0 it delete all 0 (or something similar).
I changed my code to below but same error occurred:
const bot = new TelegramBot(token, {polling: true});
userModel.find({}, function (err, users) {
if(!users) {
return
}
for(let i = 0; i < 10; i++) {
let chatId = users[i].chatId
console.log(i, chatId)
bot.getChat(chatId.toString(), function(msg) {
console.log(i, "done")
users[i].username = msg.chat.username
users[i].save()
})
}
})
Try with this syntax.
bot.getChat(chatId).then(function (msg) {
// ...
});
Thanks @sidelux! This worked! But what was problem?
@aliir74 Honestly i don't know, in my bot i use that syntax with getChatMembersCount and it works, so... btw very good ;)
I dont know, but in one place code dont work in other place - work.
Shortly speaking - all goods.
Thx.
Most helpful comment
Try with this syntax.