Setting a reminder with Slack API - javascript

I'm having trouble using the reminder method to set a reminder using the Slack API.
https://api.slack.com/methods/reminders.add
I want to set a reminder for various people in our workspace. My understanding of the Slack API docs is that a workspace token is needed for permission, which I have created. See https://api.slack.com/docs/token-types#workspace
In POSTing the reminder request, I always get the error { ok: false, error: 'user_not_specified' }. I have specified a user as part of the payload, as well as text, time and the bearer token.
Strange, because the docs say that user is optional. Removing the user from the payload results in the same error.
I'm getting the same error in both Postman and in a simple snippet using the Node Slack API library:
const { WebClient} = require('#slack/client');
const token = process.env.SLACK_TOKEN || require('./.config').SLACK_API_TOKEN;
const web = new WebClient(token);
web.reminders.add({text: 'clean up duty', time: 'in 1 minute', user: 'U0FKQ3N94'})
.then((res) => {
console.log(res);
})
.catch(console.error);
Any idea what I am doing wrong?

Related

Channel_not_found: authed_user cannot post a message to a channel via Slack API

I'm trying to post a message on a channel a user belongs via the Slack Api as an authed_user.
here is the flow:
User gives permissions with scopes 'chat:write,channels:write,channels:history'
I receive a token along with some more information from Slack that looks like xoxp-122474-a bunch of numbers
I create a Slack Client with the token and sends a request with:
const { WebClient } = require('#slack/web-api');
const client = new WebClient(token.access_token);
await client.chat.postMessage({
channel: channelId, // = Something similar to C02E2K5CCUZ
as_user: true,
text: "here is some text",
});
I get an error from the slack API, 'channel_not_found' but I checked the channel does exists + the user is in the channel.
What should I do to make this work? Am I missing anything?
Thank you !
It's possible that error is a red herring. The as_user parameter might be messing you up. That parameter can only be used for legacy apps. You can still use chat.postMessage but make sure you are also requesting the [chat:write.customize][1] scope. You will then be able to customize the posting user by defining the username and icon_urlparameters in your API call.

Slack web-api returning channel not found chat.postMessage to a private channel

I have the following code which returns me the following error. The error says channel not found, howerver I am following the docs for a private channel to either use the name of the channel or to use the string that is in the url with a G at the beginning. I have given the correct chat write permissions to the bot for the channel and am using the correct bot api generated toekn, I was able to post via webhook before but I want to use the web-api.
This is the docs I am referring to https://api.slack.com/methods/chat.postMessage#channels
const { WebClient } = require('#slack/web-api');
const token = process.env.SLACK_BOT_TOKEN;
const web = new WebClient(token);
const conversationId = 'private-channel-name'; & tried this instead 'GCV2FGELX'
const result = await web.chat.postMessage({
text: 'Hello world!',
channel: conversationId,
});
This is the error response I am getting
{
code: 'slack_webapi_platform_error',
data: {
ok: false,
error: 'channel_not_found',
response_metadata: { scopes: [Array], acceptedScopes: [Array] }
}
}
I'm not quite sure what I am doing wrong, but doing a little digging, even when I try to use the tester get request for groups.list here https://api.slack.com/methods/groups.list/test with a auth token with the correct permissions I just get a empty response back, vs a list of groups.
Not sure here.
I did note that it said this Post into any channel it has access to for what the bot can do how do I know what channel's the bot has access to. I have given it write permissions to my whole orginization?
any help would be appreciated
I too had faced the same issue.
Did you trying adding the BOT/APP to the private channel (You find this under the Channel Details >> More >> Add Apps.
Once you have added your BOT/APP to the channel. Your messages will be getting posted into the channel.
Hope this will solve your issue.
Still ran into this in 2022 with slack bolt.
The error slack_webapi_platform_error with error channel_not_found seems to mean that there is no ACCESS to this channel because app needs to be specifically added to this private channel. Thanks #Anumoy Sutradhar
Here a newer screenshot (not possible to add in comments):
The modal opens when you click on the channel name.

Firebase FCM error: 'InvalidRegistration'

I am currently trying to send a PushNotification to a Device Group using FCM with the help of Firebase Cloud Functions but once the notification is sent, it returns with code 200 but with failure :
SUCCESS response= {
multicast_id: 8834986220110966000,
success: 0,
failure: 1,
canonical_ids: 0,
results: [ { error: 'InvalidRegistration' } ]
}
Here is the code I am using to send this notification... what am I missing?
const options = {
method: 'POST',
uri: 'https://fcm.googleapis.com/fcm/send',
headers: {
'Authorization': 'key=' + serverKey,
},
body: {
to: groupId,
data: {
subject: message
},
notification: {
title: title,
body: body,
badge: 1,
},
content_available: true
},
json: true
};
return rqstProm(options)
.then((parsedBody) => {
console.log('SUCCESS response=', parsedBody);
})
.catch((err) => {
console.log('FAILED err=', err);
});
Where JSON values title, body, subject, message are String
In my case, I was sending notifications to topic ("topics/my-topic"). I was missing prepending / in the starting of topic so I was getting the same issue. SO topic should be /topics/my-topic.
May be this helps!!
There is an easier way to send a message to a device group from a Cloud Function. Use admin.messaging().sendToDeviceGroup(). Sample code and instructions are in this guide.
I think your current method is failing because there is something wrong with the group notification key provided in groupId. It should be the string key value that was returned when you created the device group. The error codes are listed in this table. For 200/InvalidRegistration it says:
Check the format of the registration token you pass to the server.
Make sure it matches the registration token the client app receives
from registering with Firebase Notifications. Do not truncate or add
additional characters.
I was losing my mind with this InvalidRegistration error.
Eventually the problem was that I was subscribing my device to "example" but sending the notification json to: "example".
But we actually need to send to "/topics/example"
2 hours of my life wasted..
A registration token is tied to a certain group of senders. When a client app registers for FCM, it must specify which senders are allowed to send messages. You should use one of those sender IDs when sending messages to the client app.
Al you need to do is add a http header 'project_id' with your sender id.
I was getting InvalidRegistration:
Basic meaning: you are using the wrong token. Why? This may happen when you a new registrationToken is given to you in onNewToken (docs), but for some reason you are using the old token. That could happen when:
You're using a different push notification library which remembers token (stores it somewhere locally) and you didn't update that library with the new token.
Your application (or other library dependencies) implements another FirebaseMessagingService, and they conflict. Only one service can accept (react to) to the action sent by the FirebaseMessaging Android library's when a new token is given to it. You can double check this by opening the AndroidManifest.xml in Android Studio and selecting the Merged Manifest tab at the bottom of the tab. You can also place debuggers in each Service from each library you use. You'll see that only one service's onNewToken gets called.
When they conflict, one doesn't get the correct token, and the FCM registration token that gets registered would be wrong. Sending a message to a wrong registration, gets you InvalidRegistration.
for me, it was a mistake that I was passing an Id from my models instead of the tokens of the users
InvalidRegistration simply means that the token is either invalid or expired. You can uninstall the app and then reinstall and get a new token and then try with that token. This will definitely solve your problem.
You can read more here.

How do I authenticate against GitHub using an API key?

I am trying to use the GitHub Javascript API but am unable to authenticate using my API key. The API Docs say I can use:
{
username: 'my-username',
password: 'my-password'
// , token: 'or an optional oAuth token'
}
But I'd like to use my API Key instead, much like I can do with curl from the command line. (for example)
curl --user "$user:$api_key" --include --request DELETE "https://api.github.com/repos/$user/$repo/labels/enhancement"
I've tried using my API Key as the token but that doesn't work.
Is use of the API Key to access GitHub via the Github API wrapper not supported? That would be weird.
Okay so it turns out what I was calling my API Key is the same thing as the personal access token and I was just being confused because
import GitHub from 'github-api'
const gh = new GitHub({
token: 'MY-PERSONAL-ACCESS-TOKEN-OBTAINED-FROM-github.com/settings/tokens' // real token redacted obviously
})
const me = gh.getUser()
console.log('me', me)
was spitting out
me User {
__apiBase: 'https://api.github.com',
__auth:
{ token: '970818535b841883ff2735fe127d289032970389',
username: undefined,
password: undefined },
__AcceptHeader: 'v3',
__authorizationHeader: 'token MY-PERSONAL-ACCESS-TOKEN-OBTAINED-FROM-github.com/settings/tokens',
__user: undefined }
and I was interpreting __user: undefined to mean that the authentication was not working.
However if I actually try then adding
me.listNotifications().then(
notifications => console.log('notifications', notifications),
err => console.log('error', err)
).catch(err => console.log('fatal', err))
tadah it works.
If I throw in a rubbish token new GitHub doesn't complain (because presumably it's not actually going off to GitHub to do the auth at that point as I first thought), but the .listNotifications() does get rejected with a 401 error.
So, that's resolved my confusion.

Google Drive API Error Daily Limit for Unauthenticated Use Exceeded

Im getting error on using Google API.
having right to connect with Google Drive and add new sheet and insert data into it.
It was working till yesterday but when i run the application today.
Im getting error :
Error appears after users given token and tried to access the DRIVE API to get all the files
domain: "usageLimits"
extendedHelp: "https://code.google.com/apis/console"
message: "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup."
reason: "dailyLimitExceededUnreg"
I have not changed any settings.
Following API are enables for my application access token. Do i have to add / enable more API to make it work.
I was using NodeJS to download a file, but was forgetting to pass the auth.
Initially I was using:
function downloadFiles() {
const service = google.drive('v3');
const dest = fs.createWriteStream('/tmp/foo.csv');
const fileId = '12345_ID_GOES_HERE';
service.files.export({
fileId: fileId,
mimeType: 'text/csv'
});
}
afterwards, I added an auth argument to the function, and passed it to the export method as well:
function downloadFiles(auth) {
const service = google.drive('v3');
const dest = fs.createWriteStream('/tmp/foo.csv');
const fileId = '12345_ID_GOES_HERE';
service.files.export({
auth: auth,
fileId: fileId,
mimeType: 'text/csv'
})
}
I am getting auth by creating an instance of google-auth-library
The problem was while getting access token. I was not providing correct scopes.
When i changed the scope to
https://spreadsheets.google.com/feeds https://docs.google.com/feeds
https://www.googleapis.com/auth/drive.file
it worked fine

Categories

Resources