Spreed: Webhook Support

Created on 3 Jun 2019  路  12Comments  路  Source: nextcloud/spreed

Is your feature request related to a problem? Please describe.
I would like to integrate my Smarthome System into Nextcloud Talk Chat for Notifications etc.

Describe the solution you'd like
Webhook Implementation (Similiar like these on Rocket.Chat/Slack etc)
Webhooks usually per Channel and multiple ones can be added for various integrations

Incoming Webhooks:
Generates an Webhook Token/Credentials to use in Scripts/Tools.
This will allow to send Messages to an specific Channel with an simple API Call. But not to read any messages for Security Reasons.

Outgoing Webhooks:
Send new Messages to an specific URL. (Standardized Format)
Optionally only if the text includes an "trigger word"

Describe alternatives you've considered
Currently im using an Dummy User and fake an normal "Web Client" to send Messages to an Channel. This basically works, but this User can also read Messages (Security Issue).

1. to develop enhancement bots & commands 馃 chat 馃挰

Most helpful comment

Having a go at implementing this, will let you know how I get on

image

All 12 comments

Having a go at implementing this, will let you know how I get on

image

Can you show us how you did the dummy User solution?

@mar565
Basically it does the same as the Browser.

  • Create an Dummy Nextcloud User
  • Create an Channel an get the Channel ID from the URL
<?

function NextcloudTalk_SendMessage($channel_id, $message) {

    $SERVER = "https://XYZ";
    $USER = "notify1";
    $PASS = "notify1pw";

    // notify hack
    $data = array(
                "token" => $channel_id,
                "message" => $message,
                "actorDisplayName" => "PYTHON-NOTIFICATION",
                "actorType" => "",
                "actorId" => "",
                "timestamp" => 0,
                "messageParameters" => array()
    );

    $payload = json_encode($data);

    $ch = curl_init($SERVER . '/ocs/v2.php/apps/spreed/api/v1/chat/' . $channel_id);

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLINFO_HEADER_OUT, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
    curl_setopt($ch, CURLOPT_USERPWD, "$USER:$PASS");
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);

    // Set HTTP Header
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'Content-Length: ' . strlen($payload),
        'Accept: application/json',
        'OCS-APIRequest: true')
    );

    $result = curl_exec($ch);
    curl_close($ch);

}

NextcloudTalk_SendMessage('jatdu2cc', 'Hello From SYMCON');

?>

Having a go at implementing this, will let you know how I get on

@BRadHoc
any news on this?

Could help. Here is how rocket chat handles webhooks. https://rocket.chat/docs/administrator-guides/integrations/gitlab/

Appologies for the long delay on this, I've had a lot of stuff going on with work and personal life.

I used a different approach to @fti7 in that I copied the functonality of the Update conversation to create a new window and the same process for putting messages into it.

It's spread across a few files, so not the simplest of solutions.

Also I believe nextcloud flow coming in server v18 will be able to cover this functionality?

If anyone is interested in @fti7 solution converted to python3, here you go:

  • Create an Dummy Nextcloud User
  • Create an Channel an get the Channel ID from the URL (in the browser)
import requests
import json

server = "https://nextcloud.xxx.com"
username = "my-bot"
password = "abc"

def NextcloudTalkSendMessage(channelId, message):
    data = {
        "token": channelId,
        "message": message,
        "actorDisplayName": "PYTHON-NOTIFICATION",
        "actorType": "",
        "actorId": "",
        "timestamp": 0,
        "messageParameters": []
    }

    url = "{}/ocs/v2.php/apps/spreed/api/v1/chat/{}".format(server, channelId)
    # print(url)
    payload = json.dumps(data);

    headers = {'content-type': 'application/json', 'OCS-APIRequest': 'true'}
    resp = requests.post(url, data=payload, headers=headers, auth=(username, password))
    print(resp)
    # print(resp.text)


NextcloudTalkSendMessage('7vijpxv5', 'Hello From Python')

However it would still be nice to have some official documentation about this or some official endpoint.

Hey, any updates on this feature, or any similar features to do with NextCloud Talk? As of now, it seems to only be possible to have automated messages sent as a result of things that occur within NextCloud, seemingly only file changes at the moment, while the original request (as well as my own interest) is about integrating an external system with NextCloud Talk, which is functionality not currently covered by Flows.

If anyone is interested in @fti7 solution converted to python3, here you go:

  • Create an Dummy Nextcloud User
  • Create an Channel an get the Channel ID from the URL (in the browser)
import requests
import json

server = "https://nextcloud.xxx.com"
username = "my-bot"
password = "abc"

def NextcloudTalkSendMessage(channelId, message):
    data = {
        "token": channelId,
        "message": message,
        "actorDisplayName": "PYTHON-NOTIFICATION",
        "actorType": "",
        "actorId": "",
        "timestamp": 0,
        "messageParameters": []
    }

    url = "{}/ocs/v2.php/apps/spreed/api/v1/chat/{}".format(server, channelId)
    # print(url)
    payload = json.dumps(data);

    headers = {'content-type': 'application/json', 'OCS-APIRequest': 'true'}
    resp = requests.post(url, data=payload, headers=headers, auth=(username, password))
    print(resp)
    # print(resp.text)


NextcloudTalkSendMessage('7vijpxv5', 'Hello From Python')

However it would still be nice to have some official documentation about this or some official endpoint.

Is this working atm?

Yes, long polling or regularly pinging the api works, but it's still on our todo to implement this properly

Yes, long polling or regularly pinging the api works, but it's still on our todo to implement this properly

Thanks for the update about this. I'm looking forward!

If anyone is interested in @fti7 solution converted to python3, here you go:

  • Create an Dummy Nextcloud User
  • Create an Channel an get the Channel ID from the URL (in the browser)
import requests
import json

server = "https://nextcloud.xxx.com"
username = "my-bot"
password = "abc"

def NextcloudTalkSendMessage(channelId, message):
    data = {
        "token": channelId,
        "message": message,
        "actorDisplayName": "PYTHON-NOTIFICATION",
        "actorType": "",
        "actorId": "",
        "timestamp": 0,
        "messageParameters": []
    }

    url = "{}/ocs/v2.php/apps/spreed/api/v1/chat/{}".format(server, channelId)
    # print(url)
    payload = json.dumps(data);

    headers = {'content-type': 'application/json', 'OCS-APIRequest': 'true'}
    resp = requests.post(url, data=payload, headers=headers, auth=(username, password))
    print(resp)
    # print(resp.text)


NextcloudTalkSendMessage('7vijpxv5', 'Hello From Python')

However it would still be nice to have some official documentation about this or some official endpoint.

possible to convert this to curl, so i can use it in bash scripts like this?

#!/usr/bin/env bash
function usage {
    programName=$0
    echo "description: use this program to post messages to Rocket.chat channel"
    echo "usage: $programName [-b \"message body\"] [-u \"rocket.chat url\"]"
    echo "      -b    The message body"
    echo "      -u    The rocket.chat hook url to post to"
    exit 1
}
while getopts ":b:u:h" opt; do
  case ${opt} in
    u) rocketUrl="$OPTARG"
    ;;
    b) msgBody="$OPTARG"
    ;;
    h) usage
    ;;
    \?) echo "Invalid option -$OPTARG" >&2
    ;;
  esac
done
if [[ ! "${rocketUrl}" || ! "${msgBody}" ]]; then
    echo "all arguments are required"
    usage
fi
read -d '' payLoad << EOF
{"text": "${msgBody}"}
EOF
echo $payLoad
statusCode=$(curl \
        --write-out %{http_code} \
        --silent \
        --output /dev/null \
        -X POST \
        -H 'Content-type: application/json' \
        --data "${payLoad}" ${rocketUrl})
echo ${statusCode}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

MarcoZehe picture MarcoZehe  路  4Comments

q-wertz picture q-wertz  路  3Comments

jakobroehrl picture jakobroehrl  路  4Comments

jospoortvliet picture jospoortvliet  路  3Comments

georgehrke picture georgehrke  路  3Comments