Skip to content

Instantly share code, notes, and snippets.

@mschmitt
Last active September 1, 2020 16:00
Show Gist options
  • Save mschmitt/4c036c10d67b45bbb1c54a7de53b7127 to your computer and use it in GitHub Desktop.
Save mschmitt/4c036c10d67b45bbb1c54a7de53b7127 to your computer and use it in GitHub Desktop.
A helper for clocking in and out of a Toggl project from a Geofency webhook
#!/usr/bin/haserl --shell=/bin/bash --accept-all
/*
A helper for clocking in and out of a Toggl project from a Geofency webhook.
Pass the following Arguments as a GET query string:
User/Password UP= (Your API token plus ":api_token")
Workspace WS=
Project PR=
Description DE=
Start/Stop SS=
Start/Stop BI=true/false (optional)
Examples for project Foo on workspace Martin:
IN: https://example.com/cgi-bin/geofency-toggl-helper?WS=Martin&PR=Foo&DE=On+site+Megacorp&BI=true&SS=start&UP=d41d8cd98f00b204e9800998ecf8427e:api_token
OUT: https://example.com/cgi-bin/geofency-toggl-helper?WS=Martin&PR=Foo&DE=On+site+Megacorp&SS=stop&UP=d41d8cd98f00b204e9800998ecf8427e:api_token
--- DEBUG OUTPUT FOLLOWS -----------------------------------------------------
geofency-toggl-helper has received the following arguments:
User/Password UP=<% echo -n $GET_UP %>
Workspace WS=<% echo -n $GET_WS %>
Project PR=<% echo -n $GET_PR %>
Description DE=<% echo -n $GET_DE %>
Start/Stop SS=<% echo -n $GET_SS %>
Billable BI=<% echo -n $GET_BI %>
*/
<%
# Receive:
# GET_UP User:Password
# GET_WS Workspace name
# GET_PR Project name
# GET_DE Description
# GET_SS start/stop
# GET_BI billable true/false
curl="curl -s -u $GET_UP"
# Get numeric workspace ID
ws_id=$($curl https://www.toggl.com/api/v8/workspaces | jq ".[] | select (.name==\"$GET_WS\").id")
[[ -z "$ws_id" ]] && echo "Can't convert workspace name $GET_WS to numeric id." && exit
# Get numeric project ID
pr_id=$($curl https://www.toggl.com/api/v8/workspaces/$ws_id/projects | jq ".[] | select (.name==\"$GET_PR\").id")
[[ -z "$pr_id" ]] && echo "Can't convert project name $GET_PR to numeric id." && exit
if [[ "$GET_SS" == 'start' ]]
then
# Clock In
printf -v postdata "{\"time_entry\":{\"description\":\"%s\",\"billable\":%s,\"pid\":%i,\"created_with\":\"geofency-toggl-helper\"}}" "$GET_DE" "${GET_BI:-false}" $pr_id
$curl -H "Content-Type: application/json" \
-d "$postdata" \
-X POST https://www.toggl.com/api/v8/time_entries/start
elif [[ "$GET_SS" == 'stop' ]]
then
# Clock Out
# Get id of currently running time entry for the project
te_id=$($curl https://www.toggl.com/api/v8/time_entries/current | jq ".[] | select (.pid==$pr_id).id")
[[ -z "$te_id" ]] && echo "Can't find timer_id for project $GET_PR ($pr_id)." && exit
$curl -H "Content-Type: application/json" \
-X PUT https://www.toggl.com/api/v8/time_entries/$te_id/stop
else
echo start/stop?
exit
fi
%>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment