Skip to content

Instantly share code, notes, and snippets.

@joshuatz
Last active February 26, 2019 09:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshuatz/c9a7c260df00d2eb406f7105c0c8a750 to your computer and use it in GitHub Desktop.
Save joshuatz/c9a7c260df00d2eb406f7105c0c8a750 to your computer and use it in GitHub Desktop.
Automagic flow for starting / stopping / resuming a Toggl timer. See https://joshuatz.com/posts/2018/flic-automagic-toggl-api-button/
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<data version="1.36.0">
<trigger type="general_broadcast">
<useDefaultName>true</useDefaultName>
<name>General Broadcast: on globalflicintent-home</name>
<enabled>true</enabled>
<action>globalflicintent-home</action>
<categoryList></categoryList>
<dataSchemeList></dataSchemeList>
<dataAuthorityList></dataAuthorityList>
<dataPathLiteralList></dataPathLiteralList>
<dataPathPrefixList></dataPathPrefixList>
<dataPathGlobList></dataPathGlobList>
<dataTypeList></dataTypeList>
<extras></extras>
<receiveInitialStickyBroadcast>false</receiveInitialStickyBroadcast>
</trigger>
<condition type="expression">
<useDefaultName>false</useDefaultName>
<name>If currEntryId is set</name>
<expression>getValue('currEntryId',false)!=false;</expression>
</condition>
<condition type="expression">
<useDefaultName>false</useDefaultName>
<name>If(lastEntry)</name>
<expression>getValue('lastEntry',false)!=false</expression>
</condition>
<condition type="expression">
<useDefaultName>false</useDefaultName>
<name>toggl_action=='resume'</name>
<expression>toggl_action=='resume'</expression>
</condition>
<action type="notification_screen">
<useDefaultName>true</useDefaultName>
<name>Notification on Screen: Flic Intent Caught!</name>
<showText>true</showText>
<text>Flic Intent Caught!</text>
<widgetName></widgetName>
<width>200</width>
<height>250</height>
<stretchToFillArea>false</stretchToFillArea>
<opacity>1.0</opacity>
<durationLong>false</durationLong>
<customPosition>false</customPosition>
<gravityType>TOP_LEFT</gravityType>
<xOffset>0</xOffset>
<yOffset>0</yOffset>
</action>
<action type="script">
<useDefaultName>false</useDefaultName>
<name>set toggl_action</name>
<script>toggl_action='resume'</script>
</action>
<action type="script">
<useDefaultName>false</useDefaultName>
<name>set togglAuth to HOME</name>
<script>// set auth header
global_togglAuth = 'Basic [YOUR_BASE64_ENCODED_AUTH_HERE]';
</script>
</action>
<action type="http_request">
<useDefaultName>false</useDefaultName>
<name>Toggl - ME (Info and latest entries)</name>
<url>https://www.toggl.com/api/v8/me?with_related_data=true</url>
<verifyCertificates>true</verifyCertificates>
<basicAuthentication>false</basicAuthentication>
<username></username>
<clientCert>false</clientCert>
<clientCertPath></clientCertPath>
<httpMethod>GET</httpMethod>
<networkType>DEFAULT</networkType>
<httpContentType>X_WWW_FORM_URLENCODED</httpContentType>
<contentType>text/plain</contentType>
<generalTextData></generalTextData>
<formFieldList>Lastname=XYZ,Firstname=ABC</formFieldList>
<uploadFieldName>upload_field</uploadFieldName>
<uploadFilePath></uploadFilePath>
<timeout>60000</timeout>
<setCustomHTTPHeaders>true</setCustomHTTPHeaders>
<customHTTPHeaders>Content-Type:application/json
Authorization:{global_togglAuth}</customHTTPHeaders>
<storeInVariable>true</storeInVariable>
<variable>response</variable>
<path>/storage/emulated/0/Download/file.bin</path>
<followRedirects>true</followRedirects>
</action>
<action type="script">
<useDefaultName>false</useDefaultName>
<name>Toggl - Parse Last Entry from ME response</name>
<script>lastEntry = false;
jsonForResumeEntry = false;
stringifiedJsonForResumeEntry = false;
responseData = fromJSON(response)['data'];
log(responseData);
log(responseData['time_entries'][0]);
if (getValue('responseData',false)!=false){
log('got responseData');
entries = responseData['time_entries'];
if(getValue('entries',false)!=false){
log('found prev toggl entries');
lastEntry = entries[length(entries)-1];
projectId = lastEntry['pid'];
jsonForResumeEntry = fromJSON('{}');
jsonForResumeEntry['time_entry'] = fromJSON('{}');
jsonForResumeEntry['time_entry']['description'] = lastEntry['description'];
jsonForResumeEntry['time_entry']['tags'] = newList('API');
jsonForResumeEntry['time_entry']['created_with'] = 'Flic + Automagic + API';
if (getValue('projectId',false)!=false &amp;&amp; projectId!=''){
jsonForResumeEntry['time_entry']['pid'] = round(projectId);
}
stringifiedJsonForResumeEntry = toJSON(jsonForResumeEntry);
}
}
log(lastEntry);</script>
</action>
<action type="script">
<useDefaultName>false</useDefaultName>
<name>Toggl - Parse togglCurrentEntry</name>
<script>currEntryId = null;
currEntryData = fromJSON(togglCurrentEntry)['data'];
if(getValue('currEntryData',false)!=false){
id = currEntryData['id'];
if (getValue('id',false)!=false){
currEntryId = id;
}
}
</script>
</action>
<action type="http_request">
<useDefaultName>false</useDefaultName>
<name>Toggl Home - get togglCurrentEntry</name>
<url>https://www.toggl.com/api/v8/time_entries/current</url>
<verifyCertificates>true</verifyCertificates>
<basicAuthentication>false</basicAuthentication>
<username></username>
<clientCert>false</clientCert>
<clientCertPath></clientCertPath>
<httpMethod>GET</httpMethod>
<networkType>DEFAULT</networkType>
<httpContentType>X_WWW_FORM_URLENCODED</httpContentType>
<contentType>text/plain</contentType>
<generalTextData></generalTextData>
<formFieldList>Lastname=XYZ,Firstname=ABC</formFieldList>
<uploadFieldName>upload_field</uploadFieldName>
<uploadFilePath></uploadFilePath>
<timeout>10000</timeout>
<setCustomHTTPHeaders>true</setCustomHTTPHeaders>
<customHTTPHeaders>Authorization:{global_togglAuth}</customHTTPHeaders>
<storeInVariable>true</storeInVariable>
<variable>togglCurrentEntry</variable>
<path>/storage/emulated/0/Download/file.bin</path>
<followRedirects>true</followRedirects>
</action>
<action type="http_request">
<useDefaultName>false</useDefaultName>
<name>Toggl Home - RESUME lastentry</name>
<url>https://www.toggl.com/api/v8/time_entries/start</url>
<verifyCertificates>true</verifyCertificates>
<basicAuthentication>false</basicAuthentication>
<username></username>
<clientCert>false</clientCert>
<clientCertPath></clientCertPath>
<httpMethod>POST</httpMethod>
<networkType>DEFAULT</networkType>
<httpContentType>GENERAL_TEXT</httpContentType>
<contentType>application/json</contentType>
<generalTextData>{stringifiedJsonForResumeEntry}</generalTextData>
<formFieldList></formFieldList>
<uploadFieldName>upload_field</uploadFieldName>
<uploadFilePath></uploadFilePath>
<timeout>20000</timeout>
<setCustomHTTPHeaders>true</setCustomHTTPHeaders>
<customHTTPHeaders>Authorization:{global_togglAuth}</customHTTPHeaders>
<storeInVariable>true</storeInVariable>
<variable>response</variable>
<path>/storage/emulated/0/Download/file.bin</path>
<followRedirects>true</followRedirects>
</action>
<action type="http_request">
<useDefaultName>false</useDefaultName>
<name>Toggl Home - Start NEW API Entry</name>
<url>https://www.toggl.com/api/v8/time_entries/start</url>
<verifyCertificates>true</verifyCertificates>
<basicAuthentication>false</basicAuthentication>
<username></username>
<clientCert>false</clientCert>
<clientCertPath></clientCertPath>
<httpMethod>POST</httpMethod>
<networkType>DEFAULT</networkType>
<httpContentType>GENERAL_TEXT</httpContentType>
<contentType>application/json</contentType>
<generalTextData>{'{"time_entry":{"description":"Working Time","tags":["API"],"created_with":"Flic + Automagic + API"}}'}</generalTextData>
<formFieldList></formFieldList>
<uploadFieldName>upload_field</uploadFieldName>
<uploadFilePath></uploadFilePath>
<timeout>20000</timeout>
<setCustomHTTPHeaders>true</setCustomHTTPHeaders>
<customHTTPHeaders>Authorization:{global_togglAuth}</customHTTPHeaders>
<storeInVariable>true</storeInVariable>
<variable>response</variable>
<path>/storage/emulated/0/Download/file.bin</path>
<followRedirects>true</followRedirects>
</action>
<action type="http_request">
<useDefaultName>false</useDefaultName>
<name>Toggl Home - STOP Current Entry</name>
<url>https://www.toggl.com/api/v8/time_entries/{currEntryId}/stop</url>
<verifyCertificates>true</verifyCertificates>
<basicAuthentication>false</basicAuthentication>
<username></username>
<clientCert>false</clientCert>
<clientCertPath></clientCertPath>
<httpMethod>PUT</httpMethod>
<networkType>DEFAULT</networkType>
<httpContentType>GENERAL_TEXT</httpContentType>
<contentType>application/json</contentType>
<generalTextData></generalTextData>
<formFieldList></formFieldList>
<uploadFieldName>upload_field</uploadFieldName>
<uploadFilePath></uploadFilePath>
<timeout>20000</timeout>
<setCustomHTTPHeaders>true</setCustomHTTPHeaders>
<customHTTPHeaders>Authorization:{global_togglAuth}</customHTTPHeaders>
<storeInVariable>true</storeInVariable>
<variable>response</variable>
<path>/storage/emulated/0/Download/file.bin</path>
<followRedirects>true</followRedirects>
</action>
<flow type="flow">
<name>Flic-Toggl-Home</name>
<enabled>true</enabled>
<executionPolicy>QUEUE</executionPolicy>
<triggercontainer id="t1" x="35.0" y="-367.5">
<trigger>General Broadcast: on globalflicintent-home</trigger>
</triggercontainer>
<actioncontainer id="t2" x="35.0" y="52.5">Notification on Screen: Flic Intent Caught!</actioncontainer>
<actioncontainer id="t3" x="35.0" y="-87.5">set toggl_action</actioncontainer>
<actioncontainer id="t4" x="35.0" y="-227.5">set togglAuth to HOME</actioncontainer>
<actioncontainer id="t5" x="35.0" y="192.5">Toggl Home - get togglCurrentEntry</actioncontainer>
<actioncontainer id="t6" x="35.0" y="332.5">Toggl - Parse togglCurrentEntry</actioncontainer>
<conditioncontainer id="t7" x="35.0" y="472.5">If currEntryId is set</conditioncontainer>
<conditioncontainer id="t8" x="140.0" y="647.5">toggl_action=='resume'</conditioncontainer>
<conditioncontainer id="t9" x="140.0" y="1172.5">If(lastEntry)</conditioncontainer>
<actioncontainer id="t10" x="350.0" y="857.5">Toggl Home - Start NEW API Entry</actioncontainer>
<actioncontainer id="t11" x="140.0" y="1347.5">Toggl Home - RESUME lastentry</actioncontainer>
<actioncontainer id="t12" x="-70.0" y="647.5">Toggl Home - STOP Current Entry</actioncontainer>
<actioncontainer id="t13" x="140.0" y="857.5">Toggl - ME (Info and latest entries)</actioncontainer>
<actioncontainer id="t14" x="140.0" y="1032.5">Toggl - Parse Last Entry from ME response</actioncontainer>
<connection from="t1" to="t4" type="NORMAL" sourcePosition="SOUTH" targetPosition="NORTH" />
<connection from="t2" to="t5" type="NORMAL" sourcePosition="SOUTH" targetPosition="NORTH" />
<connection from="t3" to="t2" type="NORMAL" sourcePosition="SOUTH" targetPosition="NORTH" />
<connection from="t4" to="t3" type="NORMAL" sourcePosition="SOUTH" targetPosition="NORTH" />
<connection from="t5" to="t6" type="NORMAL" sourcePosition="SOUTH" targetPosition="NORTH" />
<connection from="t6" to="t7" type="NORMAL" sourcePosition="SOUTH" targetPosition="NORTH" />
<connection from="t7" to="t12" type="TRUE" sourcePosition="SOUTH" targetPosition="NORTH" />
<connection from="t7" to="t8" type="FALSE" sourcePosition="SOUTH" targetPosition="NORTH" />
<connection from="t8" to="t10" type="FALSE" sourcePosition="SOUTH" targetPosition="NORTH" />
<connection from="t8" to="t13" type="TRUE" sourcePosition="SOUTH" targetPosition="NORTH" />
<connection from="t9" to="t10" type="FALSE" sourcePosition="SOUTH" targetPosition="NORTH" />
<connection from="t9" to="t11" type="TRUE" sourcePosition="SOUTH" targetPosition="NORTH" />
<connection from="t13" to="t14" type="NORMAL" sourcePosition="SOUTH" targetPosition="NORTH" />
<connection from="t14" to="t9" type="NORMAL" sourcePosition="SOUTH" targetPosition="NORTH" />
</flow>
</data>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment