Help with NextTalk Chat API

Can anyone help me with sending a new chat message using curl and API. I cannot figure out how to get the tokenā€¦

  • Method: POST
  • Endpoint: /chat/{token}
2 Likes

Well I canā€™t help you with a straight answer, but I have the same issue.
I assume your complete REST URL is something like:
https://example.org/ocs/v2.php/apps/spreed/api/v1/chat/{token}

I assume {token} is the room IT, which you can retrieve from the URL, when you select the chat room. In my case it is something like 59jpu5c9.

Though you still have to login. See here: https://docs.nextcloud.com/server/17/admin_manual//configuration_user/user_provisioning_api.html to get it all working.

Though, you can post as guest. That is what I want to do. I havenā€™t succeeded yetā€¦

1 Like

As an addition to @Bubbel s comment:
I managed to send a message as a logged-in user with curl like this.
As he said, the token is the identifier of the chat room (i found it in the URL of a chat window on my desktop). Itā€™s used twice, once in the payload and once in the url to call.

-v makes output verbose, youā€™ll want to remove that
-u uses an App-Password you need to generate beforehand for the user account you want to use.

curl -d '{"token": "<nohu63se>", "message": "hi there @<username>}' -H "Content-Type: application/json" -H "Accept: application/json" -H "OCS-APIRequest: true" -v -u "<botusername>:<Ā§%$verylongAppPasswordOfBotUser$%&>" https://<yourHost.YourUrl>/ocs/v2.php/apps/spreed/api/v1/chat/<nohu63se>

Youā€™ll have to adapt everything between <>
Thereā€™s also an issue on github about the same: https://github.com/nextcloud/spreed/issues/1879

4 Likes

Yes, this absolutely works, except for the typo (missing a double quote, between the message text and the curly brackets).

Thank you for showing the way.
Iā€™ve taken the liberty to put it all in a bash script, for easy usage, but I havenā€™t been able to post it here in a correct fashion, without it getting garbled (wrong quoting, missing backslashes, etc.

I will look into it an post when I know how.

2 Likes

Great ! it works ā€¦ thanks all

Is there a way to post an image or video to a chat? I didnā€™t see a way to do this via the Talk API. Or maybe post a URL and have it automatically ā€œunfurledā€ (previewed)? If I canā€™t upload media directly into the chat Iā€™ll probably look into uploading media with the usual nextcloud webdav API then posting a message with the URL of same in the Talk (spreed) chat.

1 Like

Does somebody managed to send Message in a room as a guest. With help of the curl command ob @sebarian I managed to send a Message as a logged in user. But I want to send message as guest with different name.

Thank you in advance!

Here is a simple chat bot i created. It listens to the chat and reply (tags whoever asked him in a conversation) with some pre programmed commands. May be helpful to someone!
Cheers.
nk-free-lab/nextcloud_chat_bot.php at master Ā· reekol/nk-free-lab Ā· GitHub

Yep. There is a way, similar to the one you described. Look at ā€˜camā€™ or ā€˜dvrā€™ section inside this script ā†’ https://github.com/reekol/nk-free-lab/blob/master/app/nextcloud_chat_bot.js

@tml89 did you made any progress on anonymous chat messages?

Nƶ, unfortunately not. But I investigated no time

Thanks again for this. The ocs/v2.php/apps/files_sharing/api/v1/shares (ā€œshare file to chatā€) endpoint you pointed me towards worked well! Users see the actual media, not just a linkā€“this is exactly what I was hoping for.

Related, for posterity: https://nextcloud-talk.readthedocs.io/en/latest/chat/#share-a-file-to-the-chat

We might also see unfurling eventually. See: External link preview Ā· Issue #3156 Ā· nextcloud/spreed Ā· GitHub

1 Like

Hi, I wonder how to get the token / room id if I use any other external app ? I am trying to test it using Postman.
Whatā€™s the step required before this?

Iā€™ll be thankful for any help.

Pardon if my English is bad, since Iā€™m not a native English speaker.

@wwe @Bubbel @tml89 I know this is an older thread and there are some others, but I just wanted to jump in on this because Iā€™ve been trying to figure this out too. The inner workings of the requests are a bit outside my knowledge base.

In Firefox I can open the developer tools, join a conversation via link, change my guest name, and send a message. Firefox sees the POST requests and can also copy them as cURL commands.

If I take that command, change the message and then leave everything else as-is, it will send a new message to the correct chat without credentials. That much works.

The bad news isā€¦ In that POST, I see headers for requesttoken and also a cookie containing an oc_sessionPassphrase. I think these must be negotiated when you join the chat session, and so it may not be possible to send a one-shot message with cURL without opening this session first.

I can continue to send messages even after leaving the page in the browser by repeating the curl command, but itā€™s not clear to me whether I can continue to use this cookie indefinitely.

Update: It looks like a short time after leaving the page, itā€™s no longer valid. Now itā€™s giving me a 404.

So for users (as already posted above) something like this works

curl -u user:password 'https://<nextcloudurl>/ocs/v2.php/apps/spreed/api/v1/chat/<token>' \
  -H 'accept: application/json, text/plain, */*' \
  -H 'cache-control: no-cache' \
  -H 'content-type: application/json' \
  -H "OCS-APIRequest: true" \
  --data-raw '{"message":"test123"}'

For guests itā€™s a bit more tricky, because guests are identified per session (based on cookies). So you first have to join the conversation (and save the cookie) and then send a message (and use the cookie). Something like this:

curl 'https://<nextcloudurl>/ocs/v2.php/apps/spreed/api/v4/room/<token>/participants/active' \
  -b nc_cookies.txt \
  -c nc_cookies.txt \
  -H 'accept: application/json, text/plain, */*' \
  -H 'content-type: application/json' \
  -H "OCS-APIRequest: true" \
  --data-raw '{"force":true}'

curl 'https://<nextcloudurl>/ocs/v2.php/apps/spreed/api/v1/chat/<token>' \
  -b nc_cookies.txt \
  -c nc_cookies.txt \
  -H 'content-type: application/json' \
  -H "OCS-APIRequest: true" \
  --data-raw '{"message":"test", "actorDisplayName": "Guest"}'

Could also have a third request to leave the conversation again.

1 Like