Sending Direct Message to a user - node.js

Right now, my bot listens to a channel in slack called 'teams' and replies in that channel. I want it to reply to the user (say 'User1') via direct message instead. How can I construct a message to do that?
Thanks!

You can send a Direct Message as follows:
var response = await activityContext.ConnectorAPI.Conversations.CreateDirectConversationAsync(activity.Recipient, activity.From);
var reply = activity.CreateReply($"This is a direct message to {activity.From.Name ?? activity.From.Id} : {activity.Text}");
reply.Conversation = new ConversationAccount(id: response.Id);
reply.ReplyToId = null;
await activityContext.ConnectorAPI.Conversations.SendToConversationAsync(reply);

Related

How can i fetch messages in a dm

I wanna fetch messages in a dm, like i can in a channel
So i have tried
<user>.messages.fetch(id)
But no luck
So what can i do to fetch messages in a dm
Full code
let id = args[0]
let react = args.slice(1).join(' ')
let fetched = message.author.messages.fetch(id)
fetched.react(react)
I tried fetching a channel from the id of the author
But also no luck
You can use message.author.dmChannel to get a DMChannel object of the particular user's DMs. Then, you can use .messages.fetch() to fetch all the messages in the channel, .messages.cache.get() to fetch a message based on its ID or .messages.cache.find() to fetch a message based on any property such as its content. An example:
const dmChannel = message.author.dmChannel
const messages = dmChannel.messages.fetch() // This gives you a list of all the message in the channel
const message = dmChannel.messages.cache.get('message-id') // This fetches the message with the particular message id given
const message = dmChannel.messages.cache.find('message-content') // This fetches the message with the particular message content given

Discord.js Network Bot : Getting the users message and sending it to the other channels

I am new to coding and I was trying to make a network bot for my server.
I am trying to have it so if someone sends a message in the network channel, then it will send that message to channels. I need some help as I have no idea what I am doing and can't find anything on Google.
I am going to set the time later but I want to get the first bit working.
Here's my code :
const network = "1022619449662124052"
const channels = [`1009882056970485852`, `1009924409714299040`]
const timeout = 1800000 // 30 minutes
client.on("messageCreate", message => {
if(message.channel.(network))// The channel that the bot watchs for a message / advertisement
let network = new Discord.MessageEmbed()
.setTitle(`Grow Togethers NetWork`)
.setDescription(`Ad Posted By : ${message.author.tag}`)
message.channel.get(channels).send(`${content}` {embed: [network]})
//${content} = the message that the user used ( advertisement )
})
Iterate through your array of ids and get the channel object. Then send to that channel
channels.forEach(id => {
const channel = message.guild.channels.cache.get(id);
channel
.send(...)
.catch(console.error);
});

Send message As a reply to specific server's channel

I have 2 guilds and the BOT is in both guilds.
The Bot works like this.
When I send a message from guild 1, the BOT copies the message and sends it to guild 2.
The requirement of making a BOT is working well but I want to make it as a reply to the message when the BOT sends it to guild 2.
Here is the CODE
const guild = client.guilds.cache.get('881014432841490452')
const channel = guild.channels.cache.find(chname => chname.name === 'general')
channel.send(message)**
If someone knows please help me.
Thanks
You cannot make a message in the same time reply to a message and make that message appear on another guild. When you reply to a message it will be on the same channel as the original message.
The only solution you have is to send the message with the URL so people can click it and access the original message:
const guild = client.guilds.cache.get('881014432841490452')
const channel = guild.channels.cache.find(c => c.name === 'general')
channel.send(`Link : ${message.url}\nContent : ${message.content}`)

Sending message to specific channel, not working

Having trouble sending a bot message on a specific channel. I very simply want to have the bot send a message to #general when it activates, to let everyone know it's working again.
In the bot.on function, I've tried the classic client.channels.get().send(), but the error messages I'm getting show that it thinks client is undefined. (It says "cannot read property 'channels' of undefined")
bot.on('ready', client => {
console.log("MACsim online")
client.channels.get('#general').send("#here");
})
The bot crashes immediately, saying: Cannot read property 'channels' of undefined
The ready event doesn't pass any client parameter.
To get a channel by name use collection.find():
client.channels.find(channel => channel.name == "channel name here");
To get a channel by ID you can simply do collection.get("ID") because the channels are mapped by their IDs.
client.channels.get("channel_id");
Here is an example I made for you:
const Discord = require("discord.js");
const Client = new Discord.Client();
Client.on("ready", () => {
let Channel = Client.channels.find(channel => channel.name == "my_channel");
Channel.send("The bot is ready!");
});
Client.login("TOKEN");
You need to get the guild / server by ID, and then the channel by ID, so your code will be something like :
const discordjs = require("discord.js");
const client = new discordjs.Client();
bot.on('ready', client => {
console.log("MACsim online")
client.guilds.get("1836402401348").channels.get('199993777289').send("#here");
})
Edit : added client call
The ready event does not pass a parameter. To gain the bot's client, simply use the variable that you assigned when you created the client.
From your code, it looks like the bot variable, where you did const bot = new Discord.Client();.
From there, using the bot (which is the bot's client), you can access the channels property!

Deleting replies from a telegram bot

I have a telegram bot written in Python. It sends message on specific commands as per mentioned in code. I want to delete the replies sent by this bot suppose after X seconds. There is a telegram bot API that deletes the message
https://api.telegram.org/botBOTID/deleteMessage?chat_id=?&message_id=?
To delete the message we need chat id and message id. To get the chat id and message id of the replied message by the bot, I need to keep reading all the messages (even from users) and find these id's. This will increase a lot of overhead on the bot.
Is there any other way to find these id's without reading all the messages?
This is the Chat object. It contains the identifier of the chat.
This is the Message object. It contains the identifier for that message and a Chat object representing the coversation where it resides.
The sendMessage REST function returns the Message you sent on success.
So your solution here is to store the Message object you get when sending a message, and then call the delete api with the parameters from the stored objects (Message.message_id and Message.chat.id).
Regarding Python you can use the pickle module to store objects in files.
In nodeJs I use these codes to delete the replies sent by bot after 10 seconds:
let TelegramBot = require('node-telegram-bot-api');
let bot = new TelegramBot(token, {polling: true});
bot.on('message', (msg) => {
let chatId = msg.chat.id;
let botReply = "A response from the bot that will removed after 10 seconds"
bot.sendMessage(chatId ,botReply)
.then((result) => { setTimeout(() => {
bot.deleteMessage(chatId, result.message_id)
}, 10 * 1000)})
.catch(err => console.log(err))
}

Resources