Last active
August 29, 2015 14:18
-
-
Save stephenkeable-allies/9762fcb385f32131e924 to your computer and use it in GitHub Desktop.
Address lookup, UK postcode validation, UK geocoding and email validation integration for Slack.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Token from slack slash command set up | |
$token = "YOUR TOKEN"; | |
// API key for PostCoder Web | |
// Sign up for your API Key at www.alliescomputing.com/postcoder/sign-up | |
// or use PCW45-12345-12345-1234X, which works with the postcode NR14 7PZ only | |
$key = "API KEY HERE"; | |
// check to ensure POST is being used | |
if (isset($_SERVER['REQUEST_METHOD']) and $_SERVER['REQUEST_METHOD'] == 'POST') { | |
// check that supplied token matches | |
if ($_POST['token'] == $token) { | |
// make sure command variable not blank | |
if (trim($_POST['text'])) { | |
// if text starts with geo run geocoding | |
if (strpos(trim($_POST['text']), 'geo', 0) !== false) { | |
// remove sub command | |
$geo_search = str_replace("geo", "", $_POST['text']); | |
// build PostCoder Web url | |
$url = "http://ws.postcoder.com/pcw/".$key."/position/uk/".rawurlencode(trim($geo_search))."?format=json"; | |
// get response from PostCoder Web | |
$response = json_decode(file_get_contents($url)); | |
// Check if response has results | |
if (count($response) > 0) { | |
// Merge all results lat,long into one message | |
$str = ""; | |
foreach ($response as $item) { | |
$str .= $item->latitude . "," . $item->longitude . "\n"; | |
} | |
// Output message | |
echo trim($str, "\n"); | |
} else { | |
// Output error if not results | |
echo "Postcode not found"; | |
} | |
// if text starts with valid run validate postcode | |
} else if (strpos(trim($_POST['text']), 'valid', 0) !== false) { | |
// remove sub command | |
$valid_search = str_replace("valid", "", $_POST['text']); | |
// build PostCoder Web url | |
$url = "http://ws.postcoder.com/pcw/".$key."/codepoint/validatepostcode/".rawurlencode(trim($valid_search))."?format=json"; | |
// get response from PostCoder Web | |
$response = json_decode(file_get_contents($url)); | |
// Check if response is true | |
if ($response) { | |
echo "Postcode is valid"; | |
} else { | |
echo "Postcode is not valid"; | |
} | |
// if text starts with email run validate email | |
} else if (strpos(trim($_POST['text']), 'email', 0) !== false) { | |
// remove sub command | |
$email_search = str_replace("email", "", $_POST['text']); | |
// build PostCoder Web url | |
$url = "http://ws.postcoder.com/pcw/".$key."/email/".rawurlencode(trim($email_search))."?format=json"; | |
// get response from PostCoder Web | |
$response = json_decode(file_get_contents($url)); | |
// Check if response has results | |
if ($response) { | |
if ($response->valid) { | |
echo "Email is valid"; | |
} else { | |
echo "Email is not valid"; | |
} | |
if ($response->warning) { | |
echo "\n" . $response->warning; | |
} | |
} else { | |
// Output error if not results | |
echo "Error occurred"; | |
} | |
// default to street postcode lookup | |
} else { | |
// build PostCoder Web url | |
$url = "http://ws.postcoder.com/pcw/".$key."/street/uk/".rawurlencode(trim($_POST['text']))."?format=json"; | |
// get response from PostCoder Web | |
$response = json_decode(file_get_contents($url)); | |
// Check if response has results | |
if (count($response) > 0) { | |
// Merge all results summarylines into one message | |
$str = ""; | |
foreach ($response as $item) { | |
$str .= $item->summaryline . "\n"; | |
} | |
// Output message | |
echo trim($str, "\n"); | |
} else { | |
// Output error if not results | |
echo "Postcode not found"; | |
} | |
} | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment