Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reengineer scripts for app engine #16

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
application: slack-integration
version: 1
runtime: php
api_version: 1

handlers:
- url: /(.*\.php)$
script: scripts/\1
55 changes: 23 additions & 32 deletions scripts/include/curl.php
Original file line number Diff line number Diff line change
@@ -1,42 +1,33 @@
<?
//HTTP Utility Methods
<?php
//HTTP Utility Methods for Google App Engine's PHP Runtime

function get_url_contents($url) {
$crl = curl_init($url);

curl_setopt($crl, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)');
curl_setopt($crl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($crl, CURLOPT_CONNECTTIMEOUT, 5);
function get_url_contents($url)
{
$context = array('http' => array('method' => 'get'));
$context = stream_context_create($context);

$response = curl_exec($crl);
curl_close($crl);
return $response;
return file_get_contents($url, FALSE, $context);
}

function get_url_contents_with_basicauth($url, $username, $password) {
$crl = curl_init();
function get_url_contents_with_basicauth($url, $username, $password)
{
$auth_header = $username . ':' . $password;
$auth_header = base64_encode($auth_header);
$auth_header = 'Authorization: Basic ' . $auth_header . "\r\n";

curl_setopt($crl, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)');
curl_setopt($crl, CURLOPT_URL, $url);
curl_setopt($crl, CURLOPT_USERPWD, "{$username}:{$password}");
curl_setopt($crl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($crl, CURLOPT_CONNECTTIMEOUT, 5);
$context = array('http' => array('method' => 'get', 'header' => $auth_header));
$context = stream_context_create($context);

$ret = curl_exec($crl);
curl_close($crl);
return $ret;
return file_get_contents($url, FALSE, $context);
}

function curl_post($uri, $data)
function curl_post($url, $data_string)
{
$crl = curl_init($uri);
curl_setopt($crl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($crl, CURLOPT_POSTFIELDS, $data);
curl_setopt($crl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($crl, CURLOPT_HTTPHEADER, array('Content-Length: ' . strlen($data)));

$response = curl_exec($crl);
curl_close($crl);
return $response;
$length_header = strlen($data_string);
$length_header = 'Content-Length: ' . $length_header . "\r\n";

$context = array('http' => array('method' => 'post', 'header' => $length_header, 'content' => $data_string));
$context = stream_context_create($context);

return file_get_contents($url, FALSE, $context);
}
?>
12 changes: 6 additions & 6 deletions scripts/include/slack.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?
//slack methods
require_once('log.php');
// require_once('log.php');

/*
Slash Command Incoming Variables
Expand Down Expand Up @@ -31,9 +31,9 @@ function BuildSlashCommand($request)


function slack_incoming_hook_post($uri, $user, $channel, $icon, $emoji, $payload){

$data = array(
"text" => $payload,
"text" => $payload,
"channel" => "#".$channel,
"username"=>$user
);
Expand All @@ -49,7 +49,7 @@ function slack_incoming_hook_post($uri, $user, $channel, $icon, $emoji, $payload

$data_string = "payload=" . json_encode($data, JSON_HEX_AMP|JSON_HEX_APOS|JSON_NUMERIC_CHECK|JSON_PRETTY_PRINT);

mylog('sent.txt',$data_string);
// mylog('sent.txt',$data_string);
return curl_post($uri, $data_string);
}

Expand All @@ -58,14 +58,14 @@ function slack_incoming_hook_post($uri, $user, $channel, $icon, $emoji, $payload
function slack_incoming_hook_post_with_attachments($uri, $user, $channel, $icon, $payload, $attachments){

$data = array(
"text" => $payload,
"text" => $payload,
"channel" => "#".$channel,
"username"=>$user,
"icon_url"=>$icon,
"attachments"=>array($attachments));

$data_string = "payload=" . json_encode($data, JSON_HEX_AMP|JSON_HEX_APOS|JSON_NUMERIC_CHECK|JSON_PRETTY_PRINT);
mylog('sent.txt',$data_string);
// mylog('sent.txt',$data_string);
return curl_post($uri, $data_string);
}

Expand Down
6 changes: 3 additions & 3 deletions scripts/meme.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

$payload = json_encode($cmd);

mylog('received.txt',$payload);
// mylog('received.txt',$payload);

$cmdText = $cmd->Text;
$memetext = str_replace("memebot ", "", $cmdText);
Expand All @@ -32,11 +32,11 @@
$bottom = urlencode($parts[2]);

$meme = CreateNewMeme($gen, $top, $bottom);
mylog('sent.txt',$meme);
// mylog('sent.txt',$meme);

$response = slack_incoming_hook_post($config['slack']['hook'], $cmd->UserName, $cmd->ChannelName, null, ":bow:", $meme);

mylog('sent.txt',$response);
// mylog('sent.txt',$response);

//str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )

Expand Down