How do i delete chats from specific user in MERN stack? - node.js

i am creating a website where user can chat with different users so now i am trying to make a delete chat button so that user can delete chat with any chat. i also built one delete account button but that's working fine as it uses deleteone query. but i am not able to use the deletemany option for the above problem.its deleting entire message database
this is the message controller
"to" used with the deletemany is the receiver id(user[1])
module.exports.deletemessage=async (req,res,next)=>{
try{
await messagemodel.deleteMany({ to: req.params.to});
}catch(ex){
next(ex)
}
}
This is the message model:
so I tried using "to" as its the only unique id of all chats with a specific user.
also its working and giving no errors but its deleting all the chats in the database.

Related

MongoDB & SMTP: What's the most efficient way to email every user

I have a Node.js & Mongoose App and I need to email every user in the MongoDB Database about a new Privacy Policy Change. I have a total of 10.000 users. What would be the most efficient & fastest way to send every user a email? I thought of a service like Postmark. Would that be a considerable option? How does Google do it to send so many emails in a short time?
My current approach (in Mongoose) would be this:
const users = await User.find({})
for (let user of users) {
// send user a email using e.g. Nodemailer...
}

Update database using Sequelize for existing field given in request Node.Js

How to update our model only for existing field given by client to our API?
Example:
User.update({‘name’: ‘value_1’, {‘email’: ‘value_2’,}.then().catch()
I want to automatically update my User model based on what my client give when Requesting my API (if they only provide name, then my Model only update the name field without updating the email field) . Is there any best practice for doing that?
You can get the user you are working with, assign the received parameters and save the user
For example:
const user = await User.findByPk(id);
Object.assign(user, params);
await user.save();

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('')

Is the Bot Framework Emulator handling new members differently from Bot Framework Webchat?

According to this official sample project (https://github.com/microsoft/BotBuilder-Samples/blob/master/samples/typescript_nodejs/13.core-bot/src/bots/dialogAndWelcomeBot.ts) I can identity new members and send them a welcome message using this (my code):
this.onMembersAdded(async (context) => {
const welcomeCardTemplate = require("../lib/data/resources/cards/welcomeCard.json");
const membersAdded = context.activity.membersAdded;
for (const member of membersAdded) {
if (member.id !== context.activity.recipient.id) {
const welcomeCard = CardFactory.adaptiveCard(welcomeCardTemplate );
await context.sendActivity({ attachments: [welcomeCard] });
}
}
});
It works great when using the emulator. As soon as I connect to the chat I get my welcome message, but when using the Chat on Azure or the WebChat it's not triggered until I first enter some kind of text input to the chat.
One thing I noticed is that when I'm using the emulator two activities are sent to the bot as soon as I connect to the chat, one that contains the Id of the bot and one that contains the Id of the user but when using the other chat options (Azure Chat and WebChat) only one activity is being sent (where the memberId is the same as the recipientId) so it never goes past the if-statement.
What am I missing here, why is only one activity being sent from the Azure Chat and WebChat?
At this time, WebChat and DirectLine behaves differently from the emulator in certain scenarios like the one you describe. There is an open issue for this particular situation where you can find more information.
As stated in the issue, there is a workaround to force the ConversationUpdate event which you can try and test if it suits your needs (I haven't tried myself).

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