Authorization error Skype Bot a day later - node.js

The bot works on such a case: the user subscribes to the event, and then the event (event generated in TeamCity) should receive a notification.
The bot works fine within one day. But at night in most cases new events are not sent to the user.
error 401 "Authorization has been denied for this request" is written in the logs when the message is sent to the user (await context.sendActivity(message))
When a user subscribes to an event, I save the link to the conversation.
const reference = TurnContext.getConversationReference(context.activity);
reference => saving to base as string (used JSON.stringify)
When I receive an event, I read the link to the conversation from the database and try to send a message.
const reference = JSON.parse(reference from base)
await adapter.continueConversation(reference, async (context) => {
try {
const reply = await context.sendActivity(message);
const reference = TurnContext
.getReplyConversationReference(context.activity, reply);
} catch (e) {
console.log(e);
}
})
3.The link to the dialogue, after sending the message, is again stored in the database
What could go wrong? Why does the code work fine during the day, and after a long time, stop?
If you send any message to the bot from any user, the messages begin to be sent again.

Related

I just made a welcome and goodbye message for my discord.js bot but it doesn't send the messages to the required channels

Here's my code,
Welcome message:
client.on('guildMemberAdd', async member => {
if(member.guild.id !== "737222740305641472")return;
const channel = guild.channels.cache.get("750952211659620413")
if (!channel) return;
let data = await canva.welcome(member, { link: "https://imgur.com/a/BPTpkDT", blur: false })
const attachment = new Discord.MessageAttachment(
data,
"welcome-image.png"
);
channel.send(
"Welcome message here",
attachment
);
});
And my goodbye message's code:
client.on('guildMemberRemove',(member) => {
if(member.guild.id !== "737222740305641472")return;
guild.channels.cache.get('781737515421138984').send(`Goodbye message here`);
});
I'd like to make it clear that I dont get any error's in my console and that I've given the bot all the perms it needs to send messages.
Also, I've already declared my guild before, so that isn't supposed to be the problem.
Can anyone tell what I'm doin wrong?
As directed by this comment, it is appearent that you have not enabled privilliged intents for your application. For your application to listen to events such as guildMemberAdd and guildMemberRemove you would have to enable to Members Intent from the Discord Developer Portal. Here is what you would be looking for:
This can be found under
Your Application > Bot > ( Scroll down ) Privilleged Gateway Intents > Server Members Intent ( toggleable ).
This was a change introduced in the Discord API version v8 for the intents to be mandatorily enabled for such API requests.

Direct Messaging custom slack bot built on node.js

I have built a slack bot using the slack/bots apis in node.js: https://slack.dev/bolt-js/tutorial/getting-started
Currently, it is working fine when I type <bot> help in a channel I have set up for using webhooks. I am trying to run those same commands in a DM with the bot using the app.event('app_mention',...) method but it is not working. its like the message doesn't register in a DM with the bot for some reason but it works in a public channel. code snippet below:
app.event('app_mention', async ({ event, client}) => {
console.log(event);
const text = event.text;
const parentMessageId = event.ts;
const eventChannel = event.channel;
if (text.includes("help")) {
console.log(event);
try {
await client.chat.postMessage({
channel: eventChannel,
text: helpMessage,
thread_ts: parentMessageId
});
} catch (err) {
console.error(err);
}
I should have permissions set up correctly as well. I basically have all the permissions that I can add for the bot
The documentation of the app_mention api specifically mentions that this event does not work with DMs.
Messages sent to your app in direct message conversations are not
dispatched via app_mention, whether the app is explicitly mentioned or
otherwise. Subscribe to message.im events to receive messages directed
to your bot user in direct message conversations.
Check here : https://api.slack.com/events/app_mention

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))
}

Messaging a user a bot does not know

I am using the Slack RTM node client and having a bit of an issue with DM's. Say a user joins the channel who has never DM'ed the bot before, the user types a command in the channel that the bot usually will respond to and by default the bot responds in a private message to the user. However, the bot cannot do this because the dataStore does not contain any DM data for this user. Code sample below...
rtm.on(RTM_EVENTS.MESSAGE, function (message) {
user = rtm.getUserById(message.user);
console.log(user); // It gets the user object fine
dm = rtm.getDMByName(user.name);
console.log(dm); // This is always undefined unless the user has DM'ed the bot previously
});
Is there a way around this? I can't seem to find anything in the docs or code to suggest there might be.
You can use the im.open method of the web API. Here's roughly how you'd do it with #slack/client (untested, apologies in advance!):
var webClient = new WebClient(token);
...
rtm.on(RTM_EVENTS.MESSAGE, function (message) {
var dm = rtm.getDMById(message.user);
if (dm) {
console.log(`Already open IM: ${dm}`);
// send a message or whatever you want to do here
} else {
webClient.im.open(message.user, function (err, result) {
var dm = result.channel.id;
console.log(`Newly opened IM: ${dm}`);
// send a message or whatever you want to do here
});
}
});

Resources