Telegram bot won't send message to a user other than who triggered the bot. [sendMessage not working] - node.js

const chat_id = req.body.message.chat.id
const dharmaraj = 2xx4xxx5x //My Telegram UID
So I have these 2 const declared at top and chat_id is ID of group/user who used my bot command. If I use the bot from my personal chat, it does reply. But when someone else use the bot in private or in group, the bot won't alert me about it in Telegram.
Here is the code:
console.log(`Post command used by ${user_id}`)
res.status(200).send({
method: 'sendMessage',
dharmaraj,
text: `Message posted successfully!!`,
parse_mode: 'Markdown'
})
The console.log() statement does work.
But I don't receive the message from the bot.
I have started the bot and have a long chat history as well. So that shouldn't be an issue.
I read this but I don't want to use any other package.
EDIT:
(1) If I replace it with the chat_id itself (ID where the bot was triggered) it will send message in that chat. Problem is to get some text in my personal chat so I know the bot has been used.
(2) I tried embedding the snippet in a try-catch but no error there.
(3) Tried replacing UID with the #username but no luck.

Related

How to send DM messages after triggering comand Dsharp+

I'm making a Discord bot and i'm in trouble. Idk how to send direct message after triggering command by user, i tried to use RequireDirectMessage, but after this command just became inactive, i couldn't trigger it neither in discord Guild nor in direct message.
You need to create a DM channel and then send a message to that channel like this
[Command("senddm"]
public async Task SendDMExample(CommandContext ctx)
{
var DMChannel = await ctx.Member.CreateDmChannelAsync();
await DMChannel.SendMessageAsync("WhateverYouAreSending");
}

How can you detect if a user replied to a message of a bot in discord.js?

I have searched the discord.js documentation but I can't find a way to make the bot detect if their own messages were replied by a user in a discord server. Is there a way to do this?
Thank you so much,
Paul10
All messages have a message_reference property. This contains the guild, channel and message id. This property is null if it isn’t a reply. I assume you already know the channel.
//async function
//message is an instance of Message
let { channel, message_reference: reply } = message
const repliedTo = await channel.messages.fetch(reply.message_id);
//repliedTo is now the replied message with author, content, etc

How to get all non bot users in discord js using a discord bot in nodejs

I have created a discord bot by taking reference from this digital ocean link.
Now I can send message to any channel using the bot but my requirement is to send dm to user of that server.
For that I have tried many SO answers and followed other links, but all the solutions end up to be same.
I have tried this two way to get the users of a guild and send dm to any one selected user.
1st way - Get all users of guild (server)
const client_notification = new Discord.Client();
client_notification.on('ready', () => {
console.log("Notification manager ready");
let guild = client_notification.guilds.cache.get("Server ID");
guild.members.cache.forEach(member => console.log("===>>>", member.user.username));
});
client_notification.login("login");
Output
Notification manager ready
===>>> discord notification
By this way it only returns me the bot name itself. Although the membersCount is 6.
2nd way - send dm to user directly (server)
client.users.cache.get('<id>').send('<message>');
It gives me undefined in output.
My configs,
Node version: 10.16.3
discord.js version: 12.5.1
My question is how to get all the guild members in discord.js?
Discord added privileged gateway intents recently. So to fetch all member data you need to enable that in the developer portal. After that you need to fetch all the members available, for that we can use the fetchAllMembers client option, and then we need to filter out bot users, so that we don't message them.
const client_notification = new Discord.Client({fetchAllMembers:true}); //Fetches all members available on startup.
client_notification.on('ready', () => {
console.log("Notification manager ready");
let guild = client_notification.guilds.cache.get("Server ID");
guild.members.cache.filter(member => !member.user.bot).forEach(member => console.log("===>>>", member.user.username));
});
client_notification.login("login");
The .filter() method filters out all the bots and gives only the real users.
You should avoid using fetchAllMembers when your bot gets larger though, as it could slow down your bot and use lots of memory.
I think the problem is related to updating the bot policy for discord. Do you have this checkbox checked in the bot settings?
https://discord.com/developers/applications
Some info about client.users.cache:
Its a cache collection, so if you restart bot, or bot never handle user message or actions before, this collection will be empty. Better use guild.members.cache.get('')

Telegram: Cannot send message/photo into a channel with Telegraf (NodeJs)

I would send a message on telegram channel with telegraf. I'v einvited the bot and put him admin.
I've tested with this code:
bot.on('text', (ctx) => {
// Explicit usage
ctx.telegram.sendMessage(ctx.message.chat.id, `Hello ${ctx.state.role}`)
// Using context shortcut
// ctx.reply(`Hello ${ctx.state.role}`)
})
bot.launch();
But it replies only if i wrote on private.
So why it doesn't work on a channel?
Than how can i send a message in that channel without a command? (For example with and interval?
I i try this one:
bot.use((ctx) => {
console.log(ctx.message)
})
when i use the bot on private chat (with him) it returns all the message data. On the channel i receive undefined
In your case CTX have current chat info, if you want to send message to channel provide correct id as documented for Telegraf sendMessage:
telegram.sendMessage(process.env.TELEGRAM_CHANNEL, ctx.message.text);
I am using a bot for a public channel, so in my case it's:
TELEGRAM_CHANNEL=#MY_PUBLIC_CHANNEL_NAME
The channel name is available in channel info settings t.me/MY_PUBLIC_CHANNEL_NAME

How to get contacts on Telegram

I use node.js module for Telegram bot.
I'm trying to get the user's contact on telegram using telegram API.
Telegram API has 2 types: Bot API and Telegram API.
I think Bot API can not get the user's contacts.
In Telegram API there is the method contact.getContacts. But I don't know how to use it.
How can I get the contacts on Telegram?
this code will give you the contact, user shares his/her contact with your bot , user types command '/special' will be prompted with button to allow bot to get contact and on agreeing in your node server you may log contact info remember to declare Markup ---->
//declare Markup
const {Extra,Markup}= Telegraf;
bot.command('special', (ctx) => {
return ctx.reply('Special buttons keyboard', Extra.markup((markup) => {
return markup.resize()
.keyboard([
markup.contactRequestButton('contact')
])
}))
})
//listens for the click on contact button
bot.on('contact', (ctx) => {
console.log(ctx.update.message.contact);
//logs { phone_number: '254*******',
//first_name: 'nelsonBlack',
//user_id: 73***** }
})
Bot API can get contact info too; I think it is easier in this case.
You can try reply keyboard with request_contact. If the user clicks it, you will receive message update with Contact field.

Resources