I'm new to Slack bots so I went through their documentation and followed some tutorials on the internet but nothing seems to help. I'm trying to add a simple bot to a workspace I've just created, all I want is to make the bot post a message once it starts. Here is my code:
const SlackBot = require('slackbots');
const botToken = 'xoxp-XXXXXXXXXXXXX-XXXXXXXXXXXX-XXXXXXXXXXXXXXX-XXXXXXXXXXXXXXXXXXXXX'
const bots = async () => {
const bot = await new SlackBot({
token: botToken,
name: 'orderbot'
});
console.log('adding event listener...');
await bot.on('start', () => {
console.log('posting message...');
bot.postMessage('general', 'Feeling hungry?');
});
};
bots();
And in the OAuth & Permissions page, I've added ALL permissions to the token's scopes. Running the bot, here is my output:
adding event listener...
/home/mohammed/OrderBot/node_modules/vow/lib/vow.js:105
throw e;
^
Error: missing_scope
at /home/mohammed/OrderBot/node_modules/slackbots/index.js:46:33
So apparently, the error is coming from the .on listener which is quite confusing and I can't understand why this is happening. What exactly am I missing?
It seems like the module slackbots which I was using is not working properly (at least for me). I solved this issue by using #slack/web-api instead.
Related
i've been trying a new project which is to code a discord bot with discord.js in node. The source code for interactions in the discord.js documentation works but, when i followed online tutorials, the bot does not reply to my messages in the discord server. please do correct me!, the bot seems to go online as my console does work. but the rest is history
const Discord = require('discord.js');
const env = require("./botconfig.json"); //json file containing tokens & IDs
const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES"] });
let prefix = env.prefix;
client.on("ready", ()=>{
console.log(`${client.user.tag} is online in ${client.guilds.cache.size} servers`)
console.log(`the prefix is ${prefix}`);
})
client.on("messageCreate", Message => {
if (Message === "ping") {
Message.channel.send('Pong.');
}
});
client.login(env.token);
On the client.on("messageCreate", the second argument is a function that takes a Message object, NOT the message as a string! (check the class here https://discord.js.org/#/docs/discord.js/stable/class/Message). You may wanna try the following:
// Access the `content` property of the message
if (Message.content === "ping") {
Message.channel.send('Pong.');
}
Also seems the tutorials you're seeing are kind of deprecated. Discord.js has a lot of new stuff to make those easier and more organized. I would suggest you to try building your bot while following the official docs which are pretty good :) https://discord.js.org/#/docs/discord.js/stable/general/welcome
The messageCreate event does not pass a string, it passes a Discord.js Message class instance, as DJS is object-oriented. Just access the .content property such as message.content.
I have a working slack app running as an Azure Function using NodeJS. It is posting payloads fine to my channel. I would also like the Function to post a message to the channel via client.chat.postMessage. As far as I can see I have set everything up correctly but whenever I attempt to post the message I get an error:
TypeError [ERR_INVALID_CHAR]: Invalid character in header content ["User-Agent"]
The code to post the message is:
const { WebClient, LogLevel } = require("#slack/web-api");
const client = new WebClient(process.env['BOT_USER_OAUTH_TOKEN'], {
// LogLevel can be imported and used to make debugging simpler
logLevel: LogLevel.DEBUG
});
const channelId = "C0319MTLHB8";
try {
// Call the chat.postMessage method using the WebClient
const result = await client.chat.postMessage({
channel: channelId,
text: "Hello world"
});
console.log(result);
} catch (error) {
context.res = {
// status: 200, /* Defaults to 200 */
body: "error: " + error
};
}
and this piece of code sits within module.exports.
I guess something doesn't like the contents of BOT_USER_OAUTH_TOKEN but this is a direct copy of the xoxb bot user oauth token. And is of the form:
xoxb-999999999999999-9999999999999-aBunchOfUpperAndLowerCaseCharacters
Any suggestions as to what I am doing wrong?
Thank you I'm Joe Too for your valuable discussed resolution. Posting as an answer to help other community members:
You missed an open bracket in const result = await client.chat.postMessage(
Glad #JimBurke, that you have solved yourself by correcting the syntax/transcription.
Isn't it Node.js 16 LTS?
I had a similar problem, But I made Node.js 14 LTS and it worked
I'm trying to create a very simple Slack bot using botkit and Google App Engine, but for some reason I keep getting 401 errors any time I message the bot. The weird thing is that the Slack Event Subscription URL (the one ending in /api/messages) validates correctly, and I get a 200 response in GAE logs and validation within Slack.
But whenever I actually message the bot it always gets a 401 error with no message explaining the error at all. I've tried various combinations of the code below, and have now stripped it down to the bare minimum as found here. Aside from dependencies and a code to decrypt credentials (which I've verified is working as expected), this is my full code at the moment:
botInit();
async function botInit () {
const credentialsRaw = await getCredentials();
const credentials = JSON.parse(credentialsRaw);
const adapter = new SlackAdapter(credentials);
const controller = new Botkit({
adapter: adapter
});
controller.on('message', async(bot, message) => {
await bot.reply(message, 'I heard a message!');
});
}
I have also tried this for the messaging function:
controller.ready(() => {
controller.hears(['hello', 'hi'], ['message', 'direct_message'],
async (bot, message) => {
await bot.reply(message, 'Meow. :smile_cat:')
})
})
and this for setting up the controller:
const controller = new Botkit({
webhook_uri: '/api/messages',
adapter: adapter
});
And everything gives back the same exact 401 error, despite all of them working with the Event Subscription URL verification on Slack.
I had same issue but figured out the problem.
I had been using Client Secret as clientSigningSecret
But I should use Signing Secret !
I'ḿ setting up a jest test suite for a Node.js and Express REST API i'm building, i'm using #firebase/testing module to initialize a testing app, however when i try to perform any sort of operation to the database this error comes out:
FIRESTORE (7.17.2) INTERNAL ASSERTION FAILED: Unexpected state
at fail (/home/cardonapablo/Documentos/Proyectos/Optica (Ilicit)../../../../../node_modules/#firebase/testing/node_modules/#firebase/firestore/src/util/assert.ts:39:9)
at hardAssert (/home/cardonapablo/Documentos/Proyectos/Optica (Ilicit)../../../../../node_modules/#firebase/testing/node_modules/#firebase/firestore/src/util/assert.ts:53:5)
at fromBytes (/home/cardonapablo/Documentos/Proyectos/Optica (Ilicit)../../../../../node_modules/#firebase/testing/node_modules/#firebase/firestore/src/remote/serializer.ts:270:5)
at fromWatchChange (/home/cardonapablo/Documentos/Proyectos/Optica (Ilicit)../../../../../node_modules/#firebase/testing/node_modules/#firebase/firestore/src/remote/serializer.ts:486:25)
at PersistentListenStream.onMessage (/home/cardonapablo/Documentos/Proyectos/Optica (Ilicit)../../../../../node_modules/#firebase/testing/node_modules/#firebase/firestore/src/remote/persistent_stream.ts:576:25)
at /home/cardonapablo/Documentos/Proyectos/Optica (Ilicit)../../../../../node_modules/#firebase/testing/node_modules/#firebase/firestore/src/remote/persistent_stream.ts:456:21
at /home/cardonapablo/Documentos/Proyectos/Optica (Ilicit)../../../../../node_modules/#firebase/testing/node_modules/#firebase/firestore/src/remote/persistent_stream.ts:509:18
at /home/cardonapablo/Documentos/Proyectos/Optica (Ilicit)../../../../../node_modules/#firebase/testing/node_modules/#firebase/firestore/src/util/async_queue.ts:369:14
I also tried connecting to my regular firestore database with the credentials i have been using to develop the endpoints and same error pops out even tho it's the app i use daily
Weird thing is, data is being written to the database, but error still stops testing
Here is firebase setup:
(src/db/functions.js)
let app = initializeTestApp({
projectId: "illicit"
})
db = app.firestore()
module.exports = { db }
Function throwing the error
(tests/fixtures/db.js)
const { db } = require('../../src/db/functions')
const bcrypt = require('bcrypt');
const createAdmin = async function() {
// Encrypt password
let encPass = await bcrypt.hash("admin", 8)
let admin = {
name: "Admin Test User",
email: "admin#test.com",
password: encPass,
tokens: []
}
// Add to db
let docRef = await db.collection('admins').add(admin) // <- This line throws the error
return;
}
module.exports = {
createAdmin
}
And finally testing file
(tests/glasses.test.js)
const supertest = require('supertest');
const app = require('../src/app')
const functions = require('./fixtures/db')
let adminToken;
let glassesId;
//Executes before any test, here is where error occurs, before any tests
beforeAll( async () => {
await functions.createAdmin()
return
})
test('Should log in an admin', async () => {
let response = await supertest(app)
.post('/admins/login')
.send({
email: 'admin#test.com',
password: 'admin'
})
.expect(200);
expect(response.body.token).toEqual(expect.any(String))
adminToken = response.token;
});
This only happens only when i try to test, regular app works just fine
Things i've tried:
Firestore rules are read and write true, so it's not a rules error
Mocked Firestore with firebase-mock and Jest seems to work fine, however this is not a
solution, since i need to test data inside the database
Hope you can help me :)
You should change Jest's test environment from the default jsdom to node using jest --env=node or by setting the testEnvironment option to node in your Jest config.
Solved the problem myself, i was using the Firebase web client, I switched to the Admin SDK made specifically for servers, i guess it was some sort of auth problem, because the admin sdk automatically authenticates you in the db
This is a open issue on GitHub. I'm pasting my comment from that issue here to hopefully help some other people:
I experienced the same error message on 9.6.6 with NextJS. I believe
this error message could be presented due to a range of errors - as I
see 100+ Stackoverflow questions with this error message.
After lots of debugging I realized I accidently used SQL
capitalization:
.orderBy('time', 'ASC') = "INTERNAL ASSERTION FAILED: Unexpected state" .orderBy('time', 'asc') = No Errors!
This was a pain to debug, and my mistake was so small. Maybe better
error reporting is needed in cases like this? When you get then Google
this error message it easily leads you down a path of debugging things
completely irrelevant to the real error.
So pretty much - a tiny syntax error can cause the error message and lead you down a road of debugging the wrong things. To solve this you have to find exactly where it is happening and narrow in your debuging.
execute this command with what is indicated a little above yarn test jest --env=node
after this the error disappears
I'm trying to make it so that when my bot gets a restart, in the ready.js file it will search for webhooks it made in all of the guilds the bot is in, the bot will then use that webhook and send messages through it. I wasn't able to get anywhere and this is in JS.
I've referred to the documentation for Discord.JS and really haven't gotten anywhere. I tried to get the client ID from webhook.owner and see if the bot's ID matches up with it. I am not sure how to extract the client ID from webhook.owner
guild.fetchWebhooks()
if(webhook.owner == `${bot.user.id}`);
(suggested)
guild.fetchWebhooks()
if(webhook.owner == `${bot.user.username}`);
(actual)
This is the only code I could come up with, can add full file if needed.
I expect a bot that when it restarts (bot.on) then it will search through all of the guilds it is in and find webhooks that it owns, and send messages through it without doing it to another active webhook.
What ends up happening is that my bot just sends out a mention of the bot.
Do something like this:
bot.on('ready', async () => { // on ready
await bot.guilds.forEach(async guild => { // in all guilds:
const webhooks = await guild.fetchWebhooks(); // check for Webhooks
await webhooks.forEach(async webhook => { // for all found Webhooks:
if (webhook.owner.id == bot.user.id) { // check if bot owns them
webhook.send('test'); // Do something with the Webhook example
}
});
});
});