Retrieving Slack messages using PHP and cURL [duplicate] - php

I'm starting out with the Slack API and trying to just get a list of messages.
Here are my steps:
Created a Slack app and gave it channels:read and channels:history scope (also re-installed it)
Queried the list of channels with conversations.list (this worked fine)
From the output of conversations.list, I found a channel that I use and copied the id
Used the conversations.history api with the channelid from step 3
Result:
{ "ok": false, "error": "not_in_channel" }
I'm not at all sure what is happening here. I definitely have messages in the channel, and the documentation page for that api does not say anything about this "not_in_channel" error code.
What am I doing wrong?

After a long time of investigations (~2 hours), I found an easy approach. For Caleb's answer, I didn't understand how to invite a Bot to the channel. Hence, I am posting this answer.
Go to your Slack Channel and type the following as a message.
/invite #BOT_NAME
Eg: If your Bot name is SRE Incident Manager the command would be as follows.
/invite #sre_incident_manager
As soon as you start typing #, Slack will automatically suggest. So it becomes easy. For this, the Bot needs to be added to your Slack Workspace.
PS: Original answer.

The error not_in_channel has the exact meaning, your custom Slack app should be added to the channel.
Exact solution 1
To resolve the error, in the Web Slack interface:
Open channel settings
Click on the Integrations tab
Click Add apps and find your custom app.
Slack app might have different interface, see Iryna Vernik's answer.
Alternative solution 2
Give access to the bot to all channels by adding workspace wide scope, for example, chat:write.public. Depends on your needs and security requirements.
Alternative solution 3
To access the channel chat from API specify Incoming webhook. Slack will generate a unique URL with the token per each channel. Only convenient for a few channels.

This error arises when you are using the bot oauth token and the bot is not invited to the channel. To solve this you need to
Invite the bot(slack app) to join the channel.
Use the OAuth Access Token instead
To add Bot to your channel you need to write /invite #Bot_name in the slack channel

I also didn't understand how to invite a Bot to the channel. Way that was proposed by Caleb and Keet was not clear for me or not working. From my side, 'invite' work after
open channel
in Details tab, choose a 'More'clause
in dropdown menu, chouse an 'add app'
in pop-up look for you app (bot)
Also i was use Bot User OAuth Access Token, because i need this functionality in private channel (additionaly, you should add for bot groups:history scope)

FOr me, instead of invite a user/bot, I invite the app.

I'm getting started with the Slack API as well, and I've come to realize that not_in_channel simply means that the user/bot you are using the token for hasn't joined that particular channel you're trying to perform an action on.
Think of it this way: if you're using Slack on the web-browser or web-app, you wouldn't be able to post a message on a channel you haven't either joined or was invited to.
โ˜๏ธ You'll also never run into this issue through the Slack UI/UX because you're not even able to access the channels UNTIL you are invited or join it.
Click to see png example of a slack message stating my bot being added to a channel
However, because we're using the API we can essentially skip some steps, and in this case we skipped the step where a user/bot has joined the channel before doing the action we're trying to perform (writing a message, grabbing information, etc).
๐Ÿ’ช How to address this
There's probably plenty of ways to do it that I'm not versed in, but if you're just concerned about a specific channel or two without the concern of scaling to x channels I'll list the way that worked for me.
๐Ÿ“‡ /invite Slash Command
As others have mentioned, putting /invite in the message box lets you use Slack's slash command shortcut to add users. What's important is this way also allows us to invite bots to the channel.
Putting "#" triggers Slack to start auto-suggesting, which is why it then becomes easy to find your bot name in the list.
Click here to see screenshot example of the /invite command with #bot_name_here
Hope this helps answer people's question on why it's happening, and thank you to the original posts that got me out of my initial mess. ๐Ÿ™

As all others said, you need to join each channel.
The bot can join channel programmatically by using API below:
https://api.slack.com/methods/conversations.join
Don't forget to add permission of conversations.join

Related

Youtube [Data-API] LiveChat where logged-in user is moderator

I'm trying to work out how i get a list of videos/streams where the user is a moderator for live chat.
This is done in nightbot and fussbot but i can't work out how they do it, it works by you adding the Nightbot channel as a moderator of your live streams chat but im unsure how they get your live stream to watch the chat from them being added as a moderator to the channel. (i'm guessing they are a channel moderator and not a video/stream as they are persistent though all casts)
I have thought it would be in the API docs but if it is i can't find it / not sure what im looking for.
I'm implementing this in PHP and so far i have it now getting stream events id's and your persistent live stream ids but i'm now stuck.
I don't think the API supports a way to retrieve a list of chats where the logged-in user is a moderator. The liveChatModerators.list method requires that a value be set for the liveChatId parameter, and that value must specify a single live chat ID.
I'll caveat the following by saying that I'm not familiar with the apps you mentioned or how they work, so this is just conjecture. Your question describes a third-party app that lets you (or helps you to) manage YouTube live chats. Presumably, to use the app, you must have granted that app permission to manage your YouTube account.
With that in mind, if you set up a live stream through that app, then the app could, while creating the live stream, also use the liveChatModerators.insert method to add its own channel as a moderator to the live stream's chat. Similarly, the app could retrieve your list of live streams and add its own channel as a moderator to any of those live streams. The app could use its own data store to determine whether its channel had already been added as a moderator for any particular chat.
Please post a comment in reply if you don't think I'm understanding your question.

how to post on google plus page stream using php/curl?

I want to post on google plus page stream using php, I checked google plus api but didn't found anything, read a post where it was saying that you can't post because google only provided read permission. When I googled I saw nextscripts SANP API to post on social networks.
If google only provided read permissions then how nextscripts is able to post?
SNAP is a paid API & I want to create my own code to do the same.
Is there anything I am missing. please guide me.
Short answer: There is no way to create a post that appears in a Google+ stream.
Medium answer: Google doesn't provide an API to post to a "normal" user's page. There are APIs that allow a website to let a user post something, and they can suggest text to post, but the user must take an explicit action. There have been attempts to use unauthorized APIs to do this, but they try to intercept the above procedure and aren't reliable. Depending on your exact needs, there are other alternatives as well.
Long answer: Depending on your exact needs, there may be some APIs which can help. But keep in mind they only apply to specific, sometimes very narrow, circumstances:
Are you trying to post to a page, not a user? There is the Google+ Page API which is available to partners only. This may be what the netxtscripts SNAP API is using - I don't know. This is what HootSuite uses, for example. You can request access at https://developers.google.com/+/api/pages-signup, but they don't approve many applicants.
Are you trying to post on behalf of a Google Apps user for other users in the same domain to see? If so, there is the Domains API at https://developers.google.com/+/domains/
Are trying to post an activity or action? Like the user listening to a song or eating at a restaurant? You may benefit from App Activities with the Moments API. These don't go directly into the stream, but are available through Search, in the user's profile in some limited ways, and they can reshare them into the stream if they wish. https://developers.google.com/+/features/app-activities
Let a user post to their stream from a web page? This still requires specific action by the user, so they have to approve what you're saying, but you can use something like normal share, +1, or interactive post buttons. https://developers.google.com/+/features/interactive-posts
You just want your server to post a plain old normal message to a normal user's stream without a person having to do anything? You can't do that.
There is still no way to post on a Google+ stream using an API. For details on what you can currently do with the limited API you can look at Google+ api
There is an issue request for this from as far back as 2011: Write access to the streams

Facebook - send chat message and/or invitation to visit an external page

I am developing an interactive website, where there is a list of users and a list of user's friends (by my website's usernames). I would like to add the possibility to connect a user to his account on facebook, in order to add the features:
a user, after indicating his facebook login credentials, should have the option to see who are his facebook friends registered is this website, and then send a chat message to someone of them with a content such as "I'm at website.com right now, join me here!".
a user should have the option to see his full contact list and then send a message (for instance a poke, an email, or other type of message... - not to the timeline!) to a subgroup inviting them to like/visit the external page;
I am more interested in the 1st option, but from what I have been reading, the facebook chat API is going to be removed by 30th april 2015 [ ref. https://developers.facebook.com/docs/chat ], with no perspectives of being replaced. Therefore I don't see other ways to implement my 1st option - is there some other way?
In respect to the 2nd option, by consulting facebook's pages [ such as: https://developers.facebook.com/docs/graph-api/reference/v2.3/friendlist, https://developers.facebook.com/docs/facebook-login/access-tokens ], I see that it is possible to get a friend list with that graph-api if I use a complicated access token. If I try to go that way, would it be possible to get the e-mails or other forms of contact after receiving those friend lists?
I would like to receive suggestions on how to implement the 1st/2nd, or both options. What are your suggestions regarding this subject?
Thank you.
(PS. different languages are welcome, although I prefer to work with php and javascript).
The Chat API is deprecated and will stop working even for old Apps after April 2015. There will be no replacement. How to invite users to an App or Website is explained here: https://developers.facebook.com/docs/apps/faq#invite_to_app
the friendlist endpoint is actually not for getting access to the friends, but only to the...well, friendlists ;) - /me/friends would be the correct endpoint, but you will only get friends who authorized your App. You can read more about that in this thread: Get ALL User Friends Using Facebook Graph API - Android
Btw, prefilling messages is not allowed anyway, even if the Chat API would still exist you would need to let the user write the invite message. So you can just use the Send/Message Dialogs. You donยดt even need to authorize the user for that.

Post on a users wall on behalf of my app (not the user)

For several years i have managed to sort things out without posting my own question, but by readings others instead. However, after reading over 200 useless posts, i've decided it's time i get some help.
I'm building a facebook app, and what i wan't to do is extremelly simple to explain. I wan't to notify a certain user about some event in real-time. For example, "the tv show you want to watch starts in an hour". Unfortunately, facebook doesn't allow apps to send private messages to users (which was my first choice). The standard way of publishing on a users wall on HIS BEHALF is not good either, because no notification is triggered. Therefore, my idea is to post on his wall on behalf of the app (or any similar action that will trigger a notification).
I know about app requests, but they are not what i'm looking for either, as you can see, they do not match what i want to do.
Also please note that the event may be particular for each user, so making a post or something for EVERYBODY to see is not an option ("Peter, you have dinner with stacy in an hour").
Firstly, i want to know if posting on the apps behalf (or facebook page, or any other idea) is possible.
If it isn't, i would like to see any other ideas to sort this out. Remember, it's extremelly important to trigger the notification.
Thanks for your time reading this, but i have one last request.
Although what i want to do is pretty simple, there are plenty of similar issues being disscused, which may lead to confusion, so please be SURE that you fully understood what i am asking before answering.
Thanks again
What about opening an event and auto inviting the specific user to the event?
Events invites trigger a notification and the only thing you need is the user's email address which you can easily get.
I am not 100% sure if an app posting on your wall initiates a notification*. Although posting to a users wall on behalf of an app is certainly possible.
You can follow the instructions on this link, under the "App Login" title, and with that retrieve the correct access token you need to make calls as your app.
In order to have the functionality you specified, you'll have to request the offline_access permission from your users - this will allow you to post to your users wall even when he/she is not currently in or using your app. (or even logged into facebook)
*
Usually these posts on behalf of an app are user initiated - ie the user pressed "post to my wall."

Cannot delete application invite in Facebook after acceptance

I've looked through this site and looked at the Facebook docs but I can't seem to delete an invite after a friend accepts.
So, what happens is:
1. Steve invites John to a game.
2. John receives that invite and clicks accept.
3. John is taken to the game, but the invite message is not delete in his facebook page.
Facebook docs suggests I add "method=delete" when I post to https://graph.facebook.com/[request_id]?access_token=xxxx
But that returns me "{"error":{"type":"OAuthException","message":"(#2) Invalid parameter: Unknown error"}}". I'm pretty sure I'm using the most recent access_token.
Can anyone provide me with sample code which deletes an application invite after a user accepts?
EDIT:
I also tried DELETE i.e. DELETE https://graph.facebook.com/[request_id]?access_token=xxxx
And I get the same error
Thanks.
There's brief examples at https://developers.facebook.com/docs/reference/dialogs/requests/ and a longer example on https://developers.facebook.com/blog/post/464/ which show how to do this.
It sounds from your comments on fjsj's answer that you know what you're supposed to be doing - it definitely looks OK to me.
It could be that you're encountering a bug of some kind, but I think in this case it's more likely that somehow the access token of the user is being sent or there's a slight error in the way you're accessing the API - If there's a specific error message you're getting back? maybe post that here if there is.
Two things to check:
Is the request you're trying to delete returned when you make a GET request to /<request id>? (using the app access token)
Are the requests being sent using the newer Requests 2.0 dialogs? If you're using the older requests system, the instructions are basically the same, but the connection you access the requests at is the [currently undocumented] /<USER>/platformrequests endpoint (as opposed to /<USER>/apprequests
Try doing a HTTP DELETE instead of a POST. This is what the documentation says.

Categories