Skip to content

Instantly share code, notes, and snippets.

@michaeltyson
Last active November 9, 2021 21:37
Show Gist options
  • Save michaeltyson/3043c4fab392c77547179806e428eba7 to your computer and use it in GitHub Desktop.
Save michaeltyson/3043c4fab392c77547179806e428eba7 to your computer and use it in GitHub Desktop.
Slack app PHP script to create todo item in Things
<?php
// Super quick-and-dirty php script to create a todo item in Things wheneven you save/star an item in Slack.
// Put this on webserver, and create a Slack app with a "star added" event subscription. See also
// https://nordicapis.com/a-beginners-guide-to-building-a-slack-bot-in-php/
// Stuff to fill in; get history key via:
// curl -H 'Authorization: Password YourPasswordHere' -v https://cloud.culturedcode.com/version/1/account/Your@Email.Here/own-history-key
$thingsHistoryKey = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'; // Required
$thingsLastEvent = '0'; // Optional
$event = file_get_contents("php://input");
$event = json_decode($event);
$itemUrl = $event->event->item->message->permalink . (isset($event->event->item->message->thread_ts) ? '?thread_ts=' . $event->event->item->message->thread_ts : '');
$title = htmlspecialchars_decode(trim($event->event->item->message->text));
$note = 'Link: '.$itemUrl;
$break = strpos($title, "\n");
if ( $break !== false ) {
$note = trim(substr($title, $break)) . "\n\n" . $note;
$title = substr($title, 0, $break);
}
if ( !$title ) {
$title = $note;
$note = null;
}
$ch=curl_init('https://cloud.culturedcode.com/version/1/history/'.$thingsHistoryKey.'/items?start-index='.$thingsLastEvent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'ThingsMac/20808500mas (x86_64; OS X 10.12.2; en_DK)');
$result = curl_exec($ch);
curl_close($ch);
$result = json_decode($result, true);
$index = $result["current-item-index"];
$now = time();
$uid = md5($title.$note);
$item = array(
"t" => 0,
"e" => "Task6",
"p" => array(
"acrd" => null,
"agr" => [],
"ar" => [],
"ato" => null,
"cd" => time(),
"dd" => null,
"dds" => null,
"dl" => [],
"do" => 0,
"icc" => 0,
"icp" => false,
"icsd" => null,
"ix" => 0,
"lai" => null,
"lt" => false,
"md" => time(),
"nt" => array(
"_t" => "tx",
"ch" => time(),
"t" => 1,
"v" => $note,
),
"pr" => [],
"rmd" => null,
"rp" => null,
"rr" => null,
"rt" => [],
"sb" => 0,
"sp" => null,
"sr" => null,
"ss" => 0,
"st" => 0,
"tg" => [],
"ti" => 0,
"tir" => null,
"tp" => 0,
"tr" => false,
"tt" => $title
)
);
$data_string = json_encode(array($uid => $item));
$ch=curl_init('https://cloud.culturedcode.com/version/1/history/'.$thingsHistoryKey.'/commit?ancestor-index='.$index.'&_cnt=1');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array(
'content-type: application/json; charset=UTF-8',
'accept: application/json',
'app-id: com.culturedcode.ThingsMac',
'content-encoding: UTF-8',
'schema: 301',
'accept-language: en-au',
'accept-encoding: gzip, deflate, br',
'app-instance-id: -com.culturedcode.ThingsMac',
'content-length: '.strlen($data_string),
'user-agent: ThingsMac/31501504 (MacBookPro15,2/x86_64; OS X 11.6.0; en_AU)',
'push-priority: 5',
)
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'ThingsMac/31501504 (MacBookPro15,2/x86_64; OS X 11.6.0; en_AU)');
$result = curl_exec($ch);
echo $result;
curl_close($ch);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment