Create Nextcloud Deck Cards via Bash Script

I created a bash Script creating Cards automatically and maybe someone can make use of it. I will use it for automatic Card Creation via cron Jobs.

#!/bin/bash
# the Syntax is you call this script and then add Board Stack User Tagsemicolaspearated Title and Comment
# You need to create a User called bot with a password you have to replace in this script lots
# you need to check your link. mine is always https so I add -k to curl and I operate on localhost

Board=${1:-DefaulBoard}
Stack=${2:-'the default Stack'}
User=${3:-DefaultUser}
Tag=${4:- "the Default Tags;Semicolonseparated"}
Titel=${5:-"the Card Title"}
Inhalt=${6:-"the Card content"}
# Nun ueberall wo noetig  die Leerzeichen rauswerfen
Titel=${Titel// /%20}
Inhalt=${Inhalt// /%20}
#echo ---- Inputs ----------------------------------------
#echo Name des Boards ist $1 oder $Board
#echo Name des Stacks ist $2 oder $Stack
#echo Name des Users ist $3 oder $User
#echo Name des Tags ist $4 oder $Tag
#echo Titel der Karte ist $5 oder $Titel
#echo Inhalt der Karte ist $6 oder $Inhalt
#echo ----------------------------------------------------
### Die Boardnummer auslesen #######################################################
#curl -u bot:yourbotpassword 'https://localhost/nextcloud/index.php/apps/deck/api/v1.0/boards' -k -H "OCS-APIRequest:true" | json_pp
# curl -s -u bot:yourbotpassword 'https://localhost/nextcloud/index.php/apps/deck/api/v1.1/boards' -k | awk -F "Arbeitswoche" '{print$1}' | rev | cut -d ":" -f 2 | rev | cut -d "," -f1
BoardId=$(curl -s -u bot:yourbotpassword 'https://localhost/nextcloud/index.php/apps/deck/api/v1.1/boards' -k | awk -F $Board '{print$1}' | rev | cut -d ":" -f 2 | rev | cut -d "," -f1)
# echo BoardID: $BoardId

### Die Stacknummer auslesen #######################################################
# Board Arbeitswoche ist Nr 3
# curl -u bot:yourbotpassword 'https://localhost/nextcloud/index.php/apps/deck/api/v1.0/boards/3/stacks' -k -H "OCS-APIRequest:true" | json_pp | grep -e 'stackId' -e '^[[:space:]]\{6\}"title' | uniq
Link="https://localhost/nextcloud/index.php/apps/deck/api/v1.1/boards/"
Link+=$BoardId
Link+="/stacks"
#curl -s -u bot:yourbotpassword $Link -k -H "OCS-APIRequest:true" | json_pp | grep -e  '^[[:space:]]\{6\}"title' -e 'stackId'
StackID=$(curl -s -u bot:yourbotpassword $Link -k -H "OCS-APIRequest:true" | awk -F "$Stack" '{print$1}' | rev | cut -d "," -f 2 | cut -d ":" -f1)
# echo StackID: $StackID

### Testen ob der Nutzer existiert ################################################
# YannickBacklog ist 7, JanBacklog ist 8
Test2=$(curl -s -u bot:yourbotpassword 'https://localhost/nextcloud/index.php/apps/deck/api/v1.0/boards' -k -H "OCS-APIRequest:true" | json_pp | grep $User  | grep "uid" |  rev | cut -d ":" -f 1| cut -c 2- | rev)
Test=${Test2:2}
if [ $Test = $User ]; then
# Sorry fuer das Gewuerge da ich musste die Gaensefuesschen wegschneiden.  Jetzt kann aber der Code in dem fi ablaufen wenn der User nicht existiert passiert nix
# echo User exists.

### Neue Karte anlegen mit Inhalt  ################################################
Link="https://localhost/nextcloud/index.php/apps/deck/api/v1.1/boards/"
Link+=$BoardId
Link+="/stacks/"
Link+=$StackID
Link+="/cards"
Titledata="title="
Titledata+=$Titel
Descdata="description="
Descdata+=$Inhalt
CardID=$(curl -s -u bot:yourbotpassword $Link -k -d $Titledata -d $Descdata -H "OCS-APIRequest: true" | json_pp | grep -e '"id"' | cut -d ':' -f 2 -s | cut -d ',' -f 1 | cut -d ' ' -f 2 )
#echo CardID: $CardID
Link+="/"
Link+=$CardID
### Der Karte ein Label/Tag zuweisen ###############################################
ModifiedTag="${Tag// /%20}" # Loop breaks with Spaces in Tags
LinkLabel=$Link
LinkLabel+="/assignLabel"
for i in ${ModifiedTag//;/ }
do
 # Repeat for each Semicolon Separated Tag
 OneTag=${i//%20/ }
 #echo $OneTag
 TagID=$(curl -s  -u bot:yourbotpassword 'https://localhost/nextcloud/index.php/apps/deck/api/v1.1/boards/3' -k  |  awk -F "labels" '{print$2}' | awk -F "owner" '{print$1}' | awk -F "$OneTag" '{print$1}' | rev | cut -d ":" -f 2 | rev | cut -d "," -f1)
 #echo TagID: $TagID
 Labeldata="labelId="
 Labeldata+=$TagID
 #echo $LinkLabel $Labeldata
 curl -s -X PUT -u  bot:yourbotpassword $LinkLabel -d $Labeldata -k -H "OCS-APIRequest: true" > /dev/null
done
### Der Karte einen User zuweisen ####################################################
LinkUser=$Link
LinkUser+="/assignUser"
Userdata="userId="
Userdata+=$User
# echo $LinkUser $Userdata
# curl -s -X PUT -u  bot:yourbotpassword $LinkUser -d $Userdata -k -H "OCS-APIRequest: true" > /dev/null
fi

1 Like

For anyone that finds this type of thing of interest, there is is a dedicated doc covering the Deck REST API. Many types of interactions and actions can be scripted against the Deck app.

2 Likes

I am done. I wanted to have a bash Script which creates Deck Cards for all my appointments next week. Here you are the parts:
Part 1 - the AC Script from above somehow optimized

#!/bin/bash
# This Script works like that: You call it and add as parameters the board, the Stack, the user, the Tags, the Title, the Commment and the Date in this funny format
# Then It calls your server (change the link down below as necessary) and figures out all the Ids. Afterwards it creates a new card with your data.
# It is possible to assign more than one Tag by delivering a Semicolaseparated List.
# create a user bot with his password to access the board and give write permissions!
# LICENSE: Free without guarantee for anything.

# Assign default parameters:
Board=${1:-Board}
Stack=${2:-'Your Stack'}
User=${3:-YourUser}
Tag=${4:-Bot}
Titel=${5:-"Test from Server"}
Inhalt=${6:-"autogenerated Card"}
Datum=${7- }
#Kill all the white spaces. Curl hates white men.
Titel=${Titel// /%20}
Inhalt=${Inhalt// /%20}
### Get the  Boardnumber #######################################################
BoardId=$(curl -s -u bot:botpw 'https://localhost/nextcloud/index.php/apps/deck/api/v1.1/boards' -k | awk -F $Board '{print$1}' | rev | cut -d ":" -f 2 | rev | cut -d "," -f1)
# echo BoardID: $BoardId

### Get the Stacknumber#######################################################
Link="https://localhost/nextcloud/index.php/apps/deck/api/v1.1/boards/"
Link+=$BoardId
Link+="/stacks"
StackID=$(curl -s -u bot:botpw $Link -k -H "OCS-APIRequest:true" | awk -F "$Stack" '{print$1}' | rev | cut -d "," -f 2 | cut -d ":" -f1)
# echo StackID: $StackID

### Testen if your Assigned User exísts ################################################
Test2=$(curl -s -u bot:botpw 'https://localhost/nextcloud/index.php/apps/deck/api/v1.0/boards' -k -H "OCS-APIRequest:true" | json_pp | grep $User  | grep "uid" |  rev | cut -d ":" -f 1| cut -c 2- | rev)
Test=${Test2:2}
if [ $Test = $User ]; then
# Sorry fuer das Gewuerge da ich musste die Gaensefuesschen wegschneiden.  Jetzt kann aber der Code in dem fi ablaufen wenn der User nicht existiert passiert nix
# echo User exists.
### Create a new Card with Title, Description and Due Date if existing ################################################
Link="https://localhost/nextcloud/index.php/apps/deck/api/v1.1/boards/"
# I know I should have done this with the basic link earlier....
Link+=$BoardId
Link+="/stacks/"
Link+=$StackID
Link+="/cards"
Titledata="title="
Titledata+=$Titel
Descdata="description="
Descdata+=$Inhalt
Duedata="duedate="
Duedata+=${Datum//:/%3A}
Duedata=${Duedata//+/%2B}
if [ "$Datum" == " " ]; then
  # No duedate assigned
  CardID=$(curl -s -u bot:botpw $Link -k -d $Titledata -d $Descdata -H "OCS-APIRequest: true" | json_pp | grep -e '"id"' | cut -d ':' -f 2 -s | cut -d ',' -f 1 | cut -d ' ' -f 2 )
else
  echo "Date converted to" $Datum " : " $Duedata
  CardID=$(curl -s -u bot:botpw $Link -k -d $Titledata -d $Descdata -d $Duedata -H "OCS-APIRequest: true" | json_pp | grep -e '"id"' | cut -d ':' -f 2 -s | cut -d ',' -f 1 | cut -d ' ' -f 2 )
fi

#echo CardID: $CardID
Link+="/"
Link+=$CardID
### Assign a Label/Tag ###############################################
ModifiedTag="${Tag// /%20}" # Loop breaks with Spaces in Tags
LinkLabel=$Link
LinkLabel+="/assignLabel"
for i in ${ModifiedTag//;/ }
do
 # Repeat for each Semicolon Separated Tag
 OneTag=${i//%20/ }
 #echo $OneTag
 TagID=$(curl -s  -u bot:botpw 'https://localhost/nextcloud/index.php/apps/deck/api/v1.1/boards/3' -k  |  awk -F "labels" '{print$2}' | awk -F "owner" '{print$1}' | awk -F "$OneTag" '{print$1}' | rev | cut -d ":" -f 2 | rev | cut -d "," -f1)
 #echo TagID: $TagID
 Labeldata="labelId="
 Labeldata+=$TagID
 #echo $LinkLabel $Labeldata
 curl -s -X PUT -u  bot:botpw $LinkLabel -d $Labeldata -k -H "OCS-APIRequest: true" > /dev/null
done
### Assign a User ####################################################
LinkUser=$Link
LinkUser+="/assignUser"
Userdata="userId="
Userdata+=$User
# echo $LinkUser $Userdata
curl -s -X PUT -u  bot:botpw $LinkUser -d $Userdata -k -H "OCS-APIRequest: true" > /dev/null
fi

The Second Script (which is called by Cron regularly Gets the appointments and hands the Data over to the Card-Creation-Script.

#!/bin/bash
# This Script calls the dav ressource of your calendar of choice. The in the Forums mentioned UID is personal for the personal calendar.
# Then we do some grinding this textblock into all the needed Data.
# There are some limitations: I always look 8 Days into the future (wanted to do the next week) but I ignore 30/31 Day shifts and the February End in the Months.
# Feel free to make a case decision, I can live with less "foresight" for 1 Day all the 2 Months.
# All the regular Appointments have usually a starting time in history, so they will not be recognized.
# Besides all one Time Appointments get found and proceded by making use of my other script which creates cards automatically using the REST API (Thanx for this Folks!)
# I add the actual time and scan the appointment name for some Info to get the Tags right. Every Card gets an "bot" Tag and the Bot-user needs to be writable for the Deck

RohCal=$(curl -s -u user:password -k 'https://localhost/nextcloud/remote.php/dav/calendars/USER/personal?export&accept=jcal' | grep -e "BEGIN:VEVENT" -e "DTSTART" -e "SUMMARY")
ModCal=${RohCal// /%20}
ModCal=$(echo "$ModCal" |sed -z 's/["\t\n\r]//g')
AktJahr=$(date -I | cut -d "-" -f1)
AktJahr=$((AktJahr))
#echo Aktuelles Jahr: $AktJahr
AktMon=$(date -I | cut -d "-" -f2)
AktMon=$((AktMon))
#echo Aktueller Monat: $AktMon
AktTag=$(date -I | cut -d "-" -f3)
AktTag=$((AktTag))
for i in ${ModCal//'BEGIN:VEVENT'/ }
do
 EventTime=$(echo $i | cut -d ":" -f2)
 EventTime=${EventTime//SUMMARY/}
 EventScope=$(echo $i | cut -d ":" -f3)
 EventScope=${EventScope//%20/ }
 Event=${i//%20/ }
 EventJahr=$(echo $EventTime | cut -c1-4)
 EventJahr=$((EventJahr))
 EventMon=$(echo $EventTime | cut -c5-6)
# EventMon=$((EventMon))
 EventTag=$(echo $EventTime | cut -c7-8)
 EventHour=$(echo $EventTime | cut -c10-11)
 EventMinute=$(echo $EventTime | cut -c12-13)
# EventTag=$((EventTag))
# echo EventJahr : $EventJahr EventMonat: $EventMon EventTag: $EventTag
### Nun das NC Format bauen
 NCDate=$EventJahr
 NCDate+="-"
 NCDate+=$EventMon
 NCDate+="-"
 NCDate+=$EventTag
 NCDate+="T"
 NCDate+=$EventHour
 NCDate+=":"
 NCDate+=$EventMinute
 NCDate+=":00"
# Check if Appointment is in the current Month or in the future this year  will cause trouble in December, I know. Merry Christmas.
 if [ $EventJahr -eq $AktJahr ] && [ $EventMon -ge $AktMon ]; then
       for j in $(seq 1 8);
           do
           SuchMon=$((AktMon))
           SuchTag=$(($AktTag+$j))
           if [ $SuchTag -gt 31 ]; then
              SuchTag=$(($AktTag-31+$j))
              SuchMon=$(($AktMon+1))
              if [ $SuchMon = 0 ]; then
              SuchMon=12
              fi
           fi
#          echo $j $SuchMon $SuchTag
           if [ $EventMon -eq $SuchMon ] && [ $EventTag -eq $SuchTag ]; then
#              echo Der Tag $EventTag waere im Monat $EventMon▒richtig
 #              echo $EventTime " : " $EventScope
               ### Nun die Kartenmacherei
#### Scan the Appointment Title for some Fragments to Assign the right Tag. (Flag)
                EventFlag="Bot"
                case $EventScope in
                          *Nextcloud*)
                            EventFlag+=";NC"
                            ;;
                          *Chaos*)
                            EventFlag+=";CCC"
                            ;;
                            # expand by gusto.
                          *)
                           ;;
                esac
#               echo $EventScope $EventTime "  time : " $NCDate
# now I call the AC Script to Create the Cards I need
/bin/bash /home/user/Scripts/AC Boardname "Stack Name" User "$EventFlag" "$EventScope" 'This Card was autogenerated' "$NCDate"
           fi
        done
 fi
done