To create a bot that automatically deletes messages containing links in a channel and reports them to the owners, you can use the always-running command (*) to check each incoming message. If a message contains a link, the bot will delete it and send a notification to the owners.
Here’s how you can implement this:
### Command: *
let request = JSON.parse(request);
if (request.channel_post) {
var msgid = request.channel_post.message_id;
var ch = request.channel_post.sender_chat.id;
var text = request.channel_post.text;
// Regular expression to detect links
var linkPattern = /(https?:\/\/[^\s]+)/g;
// Check if the message contains a link
if (linkPattern.test(text)) {
// Delete the message
HTTP.post({
url: "https://api.telegram.org/bot" + bot.token + "/deleteMessage",
body: {
chat_id: ch,
message_id: msgid
}
});
// Notify the owners about the deleted message
Api.sendMessage({
chat_id: 'YOUR_ADMIN_CHAT_ID', // Replace with your admin chat ID
text: "A message containing a link has been deleted:\n\n" +
"Channel ID: " + ch + "\n" +
"Message ID: " + msgid + "\n" +
"Text: " + text
});
}
}
### Explanation:
1. **Always Running Command (*)**: This command allows the bot to check every message sent to the channel.Link Detectionon**: A regular expression (linkPattern) is used to detect if the message contains any links.Delete Messagege**: If a link is detected, the bot sends a request to the Telegram API to delete the message.Notify Ownersrs**: After deleting the message, the bot sends a notification to the owners (replace YOUR_ADMIN_CHAT_ID with the actual chat ID of the owners).
### Important Notes:
- Ensure that the bot has the necessary permissions to delete messages in the channel.
- Replace 'YOUR_ADMIN_CHAT_ID' with the actual chat ID of the owners or admin group where you want to send the notification.
- This setup will help maintain the channel by removing unwanted links and keeping the owners informed.
Here’s how you can implement this:
### Command: *
let request = JSON.parse(request);
if (request.channel_post) {
var msgid = request.channel_post.message_id;
var ch = request.channel_post.sender_chat.id;
var text = request.channel_post.text;
// Regular expression to detect links
var linkPattern = /(https?:\/\/[^\s]+)/g;
// Check if the message contains a link
if (linkPattern.test(text)) {
// Delete the message
HTTP.post({
url: "https://api.telegram.org/bot" + bot.token + "/deleteMessage",
body: {
chat_id: ch,
message_id: msgid
}
});
// Notify the owners about the deleted message
Api.sendMessage({
chat_id: 'YOUR_ADMIN_CHAT_ID', // Replace with your admin chat ID
text: "A message containing a link has been deleted:\n\n" +
"Channel ID: " + ch + "\n" +
"Message ID: " + msgid + "\n" +
"Text: " + text
});
}
}
### Explanation:
1. **Always Running Command (*)**: This command allows the bot to check every message sent to the channel.Link Detectionon**: A regular expression (linkPattern) is used to detect if the message contains any links.Delete Messagege**: If a link is detected, the bot sends a request to the Telegram API to delete the message.Notify Ownersrs**: After deleting the message, the bot sends a notification to the owners (replace YOUR_ADMIN_CHAT_ID with the actual chat ID of the owners).
### Important Notes:
- Ensure that the bot has the necessary permissions to delete messages in the channel.
- Replace 'YOUR_ADMIN_CHAT_ID' with the actual chat ID of the owners or admin group where you want to send the notification.
- This setup will help maintain the channel by removing unwanted links and keeping the owners informed.