Created
November 14, 2018 01:38
-
-
Save lewayotte/bbdf17055417b33f6ac5e9af9edd8e7d to your computer and use it in GitHub Desktop.
Merriam Webster Word of the Day integration for Slack
This file contains hidden or 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 | |
$wotd = get_wotd(); | |
if ( !empty( $wotd ) ) { | |
send_to_slack( $wotd ); | |
} | |
function get_wotd() { | |
$rss = new DOMDocument(); | |
$rss->load( 'https://www.merriam-webster.com/wotd/feed/rss2' ); | |
$link = ''; | |
foreach ($rss->getElementsByTagName('item') as $node) { | |
$link = $node->getElementsByTagName('link')->item(0)->nodeValue; | |
break; | |
} | |
return str_replace( ' ', '%20', $link ); | |
} | |
function send_to_slack( $message ) { | |
$ch = curl_init( 'https://slack.com/api/chat.postMessage' ); | |
$data = http_build_query([ | |
'token' => 'YOURTOKEN', | |
'channel' => '#general', | |
'text' => $message, //'Hello, Foo-Bar channel message.', | |
'unfurl_links' => true | |
]); | |
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'POST' ); | |
curl_setopt( $ch, CURLOPT_POSTFIELDS, $data ); | |
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); | |
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false ); | |
$result = curl_exec( $ch ); | |
curl_close( $ch); | |
return $result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment