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))
}
Related
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}`)
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!
Using Discord.js in an Express/Node.js app, I'm trying to build a bot that grabs external data periodically and updates Discord with an embed msg containing some elements of that data. I'm trying to add a feature that will check if that data was deleted from the external source(no longer existing upon the next grab), then delete the specific msg in Discord that contains that data that was sent.
Some of the msgs posted in Discord may have duplicate data items, so I want to delete by specific msg ID, but it seems that msg ID is assigned when posted to Discord.
Is there a way to programmatically grab or return this msg ID when sending from Discord.js, rather than manually copy/pasting the msg ID from the Discord GUI? In other words, I need my bot to know which message to delete if it sees that msg's source data is no longer being grabbed.
// FOR-LOOP TO POST DATA TO DISCORD
// see if those IDs are found in persistent array
for (var i = 0; i < newIDs.length; i++) {
if (currentIDs.indexOf(newIDs[i]) == -1) {
currentIDs.push(newIDs[i]); // add to persistent array
TD.getTicket(33, newIDs[i]) // get ticket object
.then(ticket => { postDiscord(ticket); }) // post to DISCORD!
}
}
// if an ID in the persistent array is not in temp array,
// delete from persistent array & existing DISCORD msg.
// message.delete() - need message ID to get msg object...
// var msg = channel.fetchMessage(messageID) ?
Let me refer you to:
https://discord.js.org/#/docs/main/stable/class/Message
Assuming you are using async/await, you would have something like:
async () => {
let message = await channel.send(some message);
//now you can grab the ID from it like
console.log(message.id)
}
If you are going to use .then for promises, it is the same idea:
channel.send(some message)
.then(message => {
console.log(message.id)
});
ID is a property of messages, and you will only get the ID after you receive a response from the Discord API. This means you have to handle them asynchronously.
What is a correct way to synchronize Twilio chat consumption horizon and send an email notification with a list of new messages from our own server?
I can use kind of pre-hooks / post-hooks for new messages, but I don't want to keep every message and its reading status in my database.
Is there a smarter way to set up notifications on my server?
Twilio developer evangelist here.
You wouldn't need to store all of this. You can just use the REST API to get all of this information.
What you need to do is store your user's Channel Member Sid. You can then make a call to the Member resource and get their last_consumed_message_index.
With the index, which is an integer representing index of the last message the member has read within the channel, you can then call on the Message resource to list all the messages since that index. In Node, that would be so
service
.channels(CHANNEL_SID)
.members(MEMBER_SID)
.fetch()
.then(member => {
const lastConsumedMessageIndex = member.lastConsumedMessageIndex;
return service.channels(CHANNEL_SID).messages.list({
pageSize: lastConsumedMessageIndex,
limit: lastConsumedMessageIndex
});
})
.then(messages => {
console.log(messages);
// do something with unread messages
})
.catch(error => {
console.error(error);
});
Let me know if that helps at all.
I am currently using the discord.js library and node.js to make a discord bot with one function - private messaging people.
I would like it so that when a user says something like "/talkto #bob#2301" in a channel, the bot PMs #bob#2301 with a message.
So what I would like to know is... how do I make the bot message a specific user (all I know currently is how to message the author of '/talkto'), and how do I make it so that the bot can find the user it needs to message within the command. (So that /talkto #ryan messages ryan, and /talkto #daniel messages daniel, etc.)
My current (incorrect code) is this:
client.on('message', (message) => {
if(message.content == '/talkto') {
if(messagementions.users) { //It needs to find a user mention in the message
message.author.send('Hello!'); //It needs to send this message to the mentioned user
}
}
I've read the documentation but I find it hard to understand, I would appreciate any help!
The send method can be found in a User object.. hence why you can use message.author.send... message.author refers to the user object of the person sending the message. All you need to do is instead, send to the specified user. Also, using if(message.content == "/talkto") means that its only going to run IF the whole message is /talkto. Meaning, you can't have /talkto #me. Use message.content.startsWith().
client.on('message', (message) => {
if(message.content.startsWith("/talkto")) {
let messageToSend = message.content.split(" ").slice(2).join(" ");
let userToSend = message.mentions.users.first();
//sending the message
userToSend.send(messagToSend);
}
}
Example use:
/talkto #wright Hello there this is a dm!