How to get contacts on Telegram - node.js

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.

Related

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 bot won't send message to a user other than who triggered the bot. [sendMessage not working]

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.

Is there a way to make a discord bot detect if the author of the command is on a certain server? discord.js

I have a Bug report command that send me an email with the bug report (works good), but I want that the person that use were on the support server to contact him if I need more information. Is there any way to a discord bot detect if a user is in a certain server?
You could look at all the guilds the bot is in using client.guilds.cache and check if the user you want is in one or more of those guilds using guild.member().
client.guilds.cache.forEach((guild) => {
const member = guild.member(args[0]);
if (member !== null) {
message.channel.send(`${member!.guild.name} (ID: ${member!.guild.id})`);
}
});
If you're looking for checking the server when a user executes a command, you can just use message.channel.guild.id, which returns the ID of the server the command was used in. You can then compare it to a specific server ID.

Discord Welcome Channel Message

I made a discord bot and now I want to make it even more complex and I want the bot to also tell me when a user joins and leave the server in a Channel
I made the code like this and it sends only Welcome but doesn't take the users Name or ID.
Can someone help me with this, please?
client.on('guildMemberAdd', member => {
member.guild.channels.get('channel id').send("Welcome");
});
The bot sends the "Welcome" but without telling me the users Name and ID like other bots do.
client.on('guildMemberAdd', member => {
member.guild.channels.get('channel id').send("Welcome ${member.user.username} ${member.id}");
});

Respond with a rich message as a Slack bot

I'm working on a Node.js slack app containing a bot user. But I don't know how to respond to a user with a rich message as a bot.
Example:
I can respond with simple messages without problems using this code:
import * as SlackClient from '#slack/client';
slackBot = new SlackClient.RtmClient(bot_access_token)
slackBot.on(SlackClient.RTM_EVENTS.MESSAGE, message => {
slackBot.sendMessage('Hello', message.channel);
});
slackBot.start();
But RTM API doesn't support rich messages:
The RTM API only supports posting simple messages formatted using our default message formatting mode. It does not support attachments or other message formatting modes. To post a more complex message as a user clients can call the chat.postMessage Web API method with as_user set to true.
so I tried to use the web client to respond with rich messages and moved to this code:
import * as SlackClient from '#slack/client';
slackWebClient = new SlackClient.WebClient(access_token);
slackBot = new SlackClient.RtmClient(bot_access_token)
slackBot.on(SlackClient.RTM_EVENTS.MESSAGE, message => {
slackWebClient.chat.postMessage(message.channel, 'Hello', { attachments: myAttachments });
});
slackBot.start();
this approach is working when I testing the bot with my account. But if an other team user writes a DM to the bot, slackWebClient.chat.postMessage fails with the error error: channel_not_found. (I also tried to add a parameter as_user: true - but behaviour is the same, except that messages are signed with my name instead of bot name).
Solved: I created a SlackClient.WebClient instance with the Bot User OAuth Access Token instead of the OAuth Access Token (also with as_user set to true) and now it works as intended. I didn't know that the Bot User OAuth Access Token can be also used as a token for the Web API client.
Working code:
import * as SlackClient from '#slack/client';
slackWebClient = new SlackClient.WebClient(bot_access_token);
slackBot = new SlackClient.RtmClient(bot_access_token)
slackBot.on(SlackClient.RTM_EVENTS.MESSAGE, message => {
slackWebClient.chat.postMessage(message.channel, 'Hello', { attachments: myAttachments, as_user: true });
});
slackBot.start();

Resources