HTTPMOD Nextcloud Talk API [gelöst]

Begonnen von skn, 24 September 2020, 19:44:46

Vorheriges Thema - Nächstes Thema

skn

Hallo,

hier https://forum.fhem.de/index.php/topic,104849.msg987716.html#msg987716 wird beschrieben, wie man per HTTPMOD Nachrichten an Slack senden kann. Eine ähnliche API hat auch Nextcloud Talk: https://nextcloud-talk.readthedocs.io/en/latest/

Bisher konnte ich hier nirgends eine entsprechende Umsetzung finden, hat bereits jemand eine solche Definition zu laufen und wäre so hilfsbereit, diese zu teilen? Das wäre großartig; der Vorteil, keine externen Messenger-Server zu nutzen sondern seinen Eigenen, liegt ja auf der Hand.

Vielen Dank

amenomade

Das Thema NextCloud hatten wir schon: https://forum.fhem.de/index.php?topic=108225.0
Nur... die Authentifizierung von der Talk API ist nicht dokumentiert... zumindest nicht in der API Doku von Talk

Hier gibt es was: https://docs.nextcloud.com/server/19/developer_manual/client_apis/LoginFlow/index.html#login-flow-v2
Wenn das für alle APIs stimmt, sollte es mit HTTPMOD machbar sein.

Fang mal an, und ggf helfe ich dir. Ein Anfang wäre (cloud.example.com ist durch die IP oder Host zu ersetzen):
define nextcloud HTTPMOD https://cloud.example.com/ocs/v2.php/apps/spreed/api/v2/room 0
attr nextcloud enableControlSet 1
attr nextcloud enableCookies 1
attr nextcloud verbose 5

Dann ein "set nextcloud reread", und in der Log schauen

Wahrscheinlich braucht man doch ein Login, also irgendwas in der Art:
attr nextcloud reAuthRegex 401
attr nextcloud sid01URL https://cloud.example.com/index.php/login/v2
attr nextcloud sid01Data {}
attr nextcloud sid01IdRegex (?s)token":"(.*?)"
attr nextcloud sid02URL https://cloud.example.com/login/v2/poll
attr nextcloud sid02Data token=$sid
attr nextcloud sid02IdRegex (?s)appPassword":"(.*?)"

Die Doku ist unklar:
ZitatThe url in login should be opened in the default browser, this is where the user will follow the login procedure.
Das muss wahrscheinlich mit Burp oder Browser developer tools geforscht werden.
Pi 3B, Alexa, CUL868+Selbstbau 1/2λ-Dipol-Antenne, USB Optolink / Vitotronic, Debmatic und HM / HmIP Komponenten, Rademacher Duofern Jalousien, Fritz!Dect Thermostaten, Proteus

binford6000

Zitat von: amenomade am 24 September 2020, 23:23:22
Das Thema NextCloud hatten wir schon: https://forum.fhem.de/index.php?topic=108225.0
Nur... die Authentifizierung von der Talk API ist nicht dokumentiert... zumindest nicht in der API Doku von Talk

Hier gibt es was: https://docs.nextcloud.com/server/19/developer_manual/client_apis/LoginFlow/index.html#login-flow-v2
Wenn das für alle APIs stimmt, sollte es mit HTTPMOD machbar sein.

Fang mal an, und ggf helfe ich dir. Ein Anfang wäre (cloud.example.com ist durch die IP oder Host zu ersetzen):
define nextcloud HTTPMOD https://cloud.example.com/ocs/v2.php/apps/spreed/api/v2/room 0
attr nextcloud enableControlSet 1
attr nextcloud enableCookies 1
attr nextcloud verbose 5

Dann ein "set nextcloud reread", und in der Log schauen

Wahrscheinlich braucht man doch ein Login, also irgendwas in der Art:
attr nextcloud reAuthRegex 401
attr nextcloud sid01URL https://cloud.example.com/index.php/login/v2
attr nextcloud sid01Data {}
attr nextcloud sid01IdRegex (?s)token":"(.*?)"
attr nextcloud sid02URL https://cloud.example.com/login/v2/poll
attr nextcloud sid02Data token=$sid
attr nextcloud sid02IdRegex (?s)appPassword":"(.*?)"

Die Doku ist unklar: Das muss wahrscheinlich mit Burp oder Browser developer tools geforscht werden.


Hallo amenomade,
der Thread ist zwar etwas angestaubt aber trotzdem hoch interessant!  8)

Falls dein Angebot zum Forschen noch steht - ich hätte soweit alles am Start:
HTTPMOD von oben, Nextcloud mit Talk und ein V5 Log vom reread:

2021.11.01 17:26:35.714 5: nextcloud: set called with reread
2021.11.01 17:26:35.714 4: nextcloud: GetUpdate called (reread)
2021.11.01 17:26:35.715 5: nextcloud: AddToQueue adds type update to URL https://nc.moebus.co/ocs/v2.php/apps/spreed/api/v2/room, no data, no headers, retry 0, initial queue len: 0
2021.11.01 17:26:35.715 5: nextcloud: HandleSendQueue called from AddToSendQueue, qlen = 1
2021.11.01 17:26:35.716 5: nextcloud: HandleSendQueue is using Cookie __Host-nc_sameSiteCookielax with path / and Value true (key __Host-nc_sameSiteCookielax;/, destination path is /ocs/v2.php/apps/spreed/api/v2/room)
2021.11.01 17:26:35.716 5: nextcloud: HandleSendQueue is using Cookie __Host-nc_sameSiteCookiestrict with path / and Value true (key __Host-nc_sameSiteCookiestrict;/, destination path is /ocs/v2.php/apps/spreed/api/v2/room)
2021.11.01 17:26:35.716 5: nextcloud: HandleSendQueue is using Cookie oc_sessionPassphrase with path / and Value o%2FIVTQboGVSn2qN49Q74%2F9Zyb%2FzKDQhMeUBBY1iPDf%2FqnggFJABw2oN%2B0PLYejGNXMur%2F7XLwLv4S7qNajDpLOJTHM3tuJ9Ae4yEssiAonKvZcEWfED8mCSTY5UbmCGn (key oc_sessionPassphrase;/, destination path is /ocs/v2.php/apps/spreed/api/v2/room)
2021.11.01 17:26:35.716 5: nextcloud: HandleSendQueue is using Cookie ocq1xvwsv1lo with path / and Value g35cvcp43lk6vrod34tqnlr0qn (key ocq1xvwsv1lo;/, destination path is /ocs/v2.php/apps/spreed/api/v2/room)
2021.11.01 17:26:35.716 5: nextcloud: DoCookies is adding Cookie header: __Host-nc_sameSiteCookielax=true; __Host-nc_sameSiteCookiestrict=true; oc_sessionPassphrase=o%2FIVTQboGVSn2qN49Q74%2F9Zyb%2FzKDQhMeUBBY1iPDf%2FqnggFJABw2oN%2B0PLYejGNXMur%2F7XLwLv4S7qNajDpLOJTHM3tuJ9Ae4yEssiAonKvZcEWfED8mCSTY5UbmCGn; ocq1xvwsv1lo=g35cvcp43lk6vrod34tqnlr0qn
2021.11.01 17:26:35.716 4: nextcloud: HandleSendQueue sends update with timeout 2 to https://nc.moebus.co/ocs/v2.php/apps/spreed/api/v2/room, No Data,
header: Cookie: __Host-nc_sameSiteCookielax=true; __Host-nc_sameSiteCookiestrict=true; oc_sessionPassphrase=o%2FIVTQboGVSn2qN49Q74%2F9Zyb%2FzKDQhMeUBBY1iPDf%2FqnggFJABw2oN%2B0PLYejGNXMur%2F7XLwLv4S7qNajDpLOJTHM3tuJ9Ae4yEssiAonKvZcEWfED8mCSTY5UbmCGn; ocq1xvwsv1lo=g35cvcp43lk6vrod34tqnlr0qn
2021.11.01 17:26:35.955 5: nextcloud: ReadCallback called from __ANON__
2021.11.01 17:26:35.955 4: nextcloud: Read callback: request type was update retry 0,
header: HTTP/1.1 404 Not Found
Server: openresty
Date: Mon, 01 Nov 2021 16:26:35 GMT
Content-Type: text/xml; charset=UTF-8
Connection: close
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate
Pragma: no-cache
Content-Security-Policy: default-src 'self'; script-src 'self' 'nonce-TFo3ZzNqcU9hdDk5bU01M1JWQy9vL3RwVTkwVXV4RkhHemVPMUJQL0wyVT06WnR1T2xGUDJQYkFJcmZaT0RnQ0t3cDhJRjY0anlrc0plV1hIaEh5Y1ppST0='; style-src 'self' 'unsafe-inline'; frame-src *; img-src * data: blob:; font-src 'self' data:; media-src *; connect-src *; object-src 'none'; base-uri 'self';
Set-Cookie: ocq1xvwsv1lo=u4dfs8vdnkha1h4ii2c5kncaf6; path=/; secure; HttpOnly; SameSite=Lax
Referrer-Policy: no-referrer
X-Content-Type-Options: nosniff
X-Download-Options: noopen
X-Frame-Options: SAMEORIGIN
X-Permitted-Cross-Domain-Policies: none
X-Robots-Tag: none
X-XSS-Protection: 1; mode=block
Strict-Transport-Security: max-age=15552000;includeSubdomains, body length 286
2021.11.01 17:26:35.956 5: nextcloud: Read callback: body
<?xml version="1.0"?>
<ocs>
<meta>
  <status>failure</status>
  <statuscode>404</statuscode>
  <message>Invalid query, please check the syntax. API specifications are here: http://www.freedesktop.org/wiki/Specifications/open-collaboration-services.
</message>
</meta>
<data/>
</ocs>

2021.11.01 17:26:35.956 4: nextcloud: BodyDecode is not decoding the response body (charset UTF-8, bodyDecode defaults to none)
2021.11.01 17:26:35.956 5: nextcloud: GetCookies is looking for Cookies
2021.11.01 17:26:35.956 4: nextcloud: GetCookies parsed Cookie: ocq1xvwsv1lo Wert u4dfs8vdnkha1h4ii2c5kncaf6 Rest path=/; secure; HttpOnly; SameSite=Lax
2021.11.01 17:26:35.956 5: nextcloud: ExtractSid called, context reading, num unknown
2021.11.01 17:26:35.957 4: nextcloud: checking for redirects, code=404, ignore=0
2021.11.01 17:26:35.957 4: nextcloud: no redirects to handle
2021.11.01 17:26:35.957 5: nextcloud: Read callback sets LAST_REQUEST to update
2021.11.01 17:26:35.957 5: nextcloud: CheckAuth is checking buffer with ReAuthRegex (?^:401)
2021.11.01 17:26:35.957 5: nextcloud: CheckAuth decided no authentication required
2021.11.01 17:26:35.957 5: nextcloud: Read starts parsing response to update with defined readings:
2021.11.01 17:26:35.957 4: nextcloud: Read response to update didn't match any Reading
2021.11.01 17:26:35.957 5: nextcloud: HandleSendQueue called from ReadCallback, qlen = 0
2021.11.01 17:26:35.958 5: nextcloud: HandleSendQueue found no usable entry in queue


header: HTTP/1.1 404 Not Found kommt vermutlich daher, dass das token für die sid nicht korrekt geparst wird...

VG Sebastian

skn

Zitat von: amenomade am 24 September 2020, 23:23:22
Das Thema NextCloud hatten wir schon: https://forum.fhem.de/index.php?topic=108225.0
Nur... die Authentifizierung von der Talk API ist nicht dokumentiert... zumindest nicht in der API Doku von Talk

Hier gibt es was: https://docs.nextcloud.com/server/19/developer_manual/client_apis/LoginFlow/index.html#login-flow-v2
Wenn das für alle APIs stimmt, sollte es mit HTTPMOD machbar sein.

Fang mal an, und ggf helfe ich dir. Ein Anfang wäre (cloud.example.com ist durch die IP oder Host zu ersetzen):
define nextcloud HTTPMOD https://cloud.example.com/ocs/v2.php/apps/spreed/api/v2/room 0
attr nextcloud enableControlSet 1
attr nextcloud enableCookies 1
attr nextcloud verbose 5

Dann ein "set nextcloud reread", und in der Log schauen

Wahrscheinlich braucht man doch ein Login, also irgendwas in der Art:
attr nextcloud reAuthRegex 401
attr nextcloud sid01URL https://cloud.example.com/index.php/login/v2
attr nextcloud sid01Data {}
attr nextcloud sid01IdRegex (?s)token":"(.*?)"
attr nextcloud sid02URL https://cloud.example.com/login/v2/poll
attr nextcloud sid02Data token=$sid
attr nextcloud sid02IdRegex (?s)appPassword":"(.*?)"

Die Doku ist unklar: Das muss wahrscheinlich mit Burp oder Browser developer tools geforscht werden.


Hallo und Danke und sorry für die späte Rückantwort; hatte die Benachrichtigungen nicht eingschaltet und mein Post hier ist offenbar meiner angehenden Alsheimer zum Opfer gefallen..
Laut dieser Info https://castrillo.gitlab.io/figaro/post/bot-nextcloud-api/ könnte (im Zusammenhang mit den Headern?) die Authentifizierung auch einfacher zu bewerkstelligen sein?

curl -d '{"token":"tokenid", "message":"hello world"}' -H "Content-Type: application/json" -H "Accept:application/json" -H "OCS-APIRequest:true" -u "user:password" https://host.domain/ocs/v1.php/apps/spreed/api/v1/chat/tokenid 2> /dev/null | > /dev/null


Habe dies erfolgreich getestet, die Umsetzung in HTTPMOD leider nicht...
Vielen Dank


skn

#4
Manchmal sieht man (lange) den Baum vor lauter Bäumen nicht. So funktioniert das Senden von Textnachrichten an 3 auswählbare Unterhaltungen in Talk mit einem User "dummy", welcher die Nachrichten an die jeweilge Unterhaltung sendet (dummy muss zuvor zu den Unterhaltungen hinzugefügt werden):

define Talk HTTPMOD none 0
attr Talk set01Data {"token":"unterhaltungs-id1", "message":"$val"}
attr Talk set01Name unterhaltung_1
attr Talk set01TextArg 1
attr Talk set01URL https://dummy:dummy_password@example.next.cloud/ocs/v2.php/apps/spreed/api/v1/chat/unterhaltungs-id1
attr Talk set02Data {"token":"unterhaltungs-id2", "message":"$val"}
attr Talk set02Name unterhaltung_2
attr Talk set02TextArg 1
attr Talk set02URL https://dummy:dummy_password@example.next.cloud/ocs/v2.php/apps/spreed/api/v1/chat/unterhaltungs-id2
attr Talk set03Data {"token":"unterhaltungs-id3", "message":"$val"}
attr Talk set03Name unterhaltung_3
attr Talk set03TextArg 1
attr Talk set03URL https://dummy:dummy_password@example.next.cloud/ocs/v2.php/apps/spreed/api/v1/chat/unterhaltungs-id3
attr Talk setHeader1 Content-Type: application/json
attr Talk setHeader2 Accept: application/json
attr Talk setHeader3 OCS-APIRequest: true


Die unterhaltungs-id kann einfach aus der URL der Unterhaltung entnommen werden, diese hat das Schema https://example.next.cloud/call/unterhaltungs-id

kamp

Danke, funktioniert prächtig. Für alle die von wo anders (z.B. TelegramBot) kommen, ein Pro-Tipp: Ändert den Name "unterhaltung_1" z.B. auf "msg", dann müsst ihr in euren bestehenden Befehlen nur "myTelegramBot" durch "Talk" ersetzen und ihr habt alles schnell und unkompliziert auf Talk umgestellt.

Ich hätte noch eine Frage, kann man damit auch eingehende Talk-Nachrichten in FHEM irgendwie verarbeiten? Das wäre dann noch das On-Top Feature, denn derzeit muss ich Telegram noch für eingehende Befehle nutzen.