Skip to content

Instantly share code, notes, and snippets.

@levelsio
Created April 19, 2018 08:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save levelsio/601304e16a842ddb4fc4ce8f4c9b1e91 to your computer and use it in GitHub Desktop.
Save levelsio/601304e16a842ddb4fc4ce8f4c9b1e91 to your computer and use it in GitHub Desktop.
<?
// this bot tries to match chat messages to previous forum posts and links to them
require_once(__DIR__.'/../app/config.php');
require_once(__DIR__.'/../app/functions.php');
loadDbs(array('users_global','messages','questions'));
if($cache=file_get_contents(__DIR__.'/../data/cities.serialized')){} else {
exit(55);
}
$cache=unserialize($cache);
$cities=$cache['cities'];
echo "Finding questions in recent messages...";
echo "\n";
$wordsToRemoveToMatch=array(
'which','really','need','figure out','find','out','best','p.s','btw','by the way','where','i','you','he','we','can','know',"what's",'how','where','to','an','a','are','most','there','any','for','by','anyone','who','what','that','there','get','is','was','be','in','on','the','remote','remotely','find','good','+'
);
$query=$messagesDb->prepare("SELECT * FROM messages WHERE team='nomadlist' AND message LIKE '%?' AND epoch > ".(strtotime("-1 minute")*1000));
if($query->execute()) {} else {
exit(68);
}
$messages=$query->fetchAll(PDO::FETCH_ASSOC);
$query=$questionsDb->prepare("SELECT id,question FROM questions ORDER BY epoch ASC");
$query->execute();
$questions=$query->fetchAll(PDO::FETCH_ASSOC);
$alreadySentToSlack=array();
foreach($messages as $message) {
$words=explode(' ',strtolower($message['message']));
$newString='';
foreach($words as $word) {
if(!in_array($word,$wordsToRemoveToMatch)) {
$newString=$newString.' '.$word;
}
}
$newString=trim($newString);
$message['messageCleaned']=$newString;
$message['messageCleaned']=str_replace('-',' ',makeUrlSlug($message['messageCleaned']));
// if message does NOT contain the city channel itself, add it
if(stripos(makeUrlSlug($message['messageCleaned']),$message['channel'])===false) {
$message['messageCleaned']=$message['messageCleaned'].' '.str_replace('-',' ',$message['channel']);
}
// check questions
$answeredQuestionId='';
foreach($questions as $question) {
$words=explode(' ',strtolower($question['question']));
$newString='';
foreach($words as $word) {
if(!in_array($word,$wordsToRemoveToMatch)) {
$newString=$newString.' '.$word;
}
}
$newString=trim($newString);
$question['questionCleaned']=$newString;
$message['questionCleaned']=str_replace('-',' ',makeUrlSlug($message['questionCleaned']));
$percent1=0;
$percent2=0;
similar_text($question['questionCleaned'],$message['messageCleaned'],$percent1);
similar_text($message['messageCleaned'],$question['questionCleaned'],$percent2);
if($percent1>75 || $percent2 >75) {
echo $message['message'];
echo "\t<->\t";
echo $question['question'];
echo "\n\n";
$answeredQuestion=$question['question'];
$answeredQuestionId=$question['id'];
break;
}
}
if(empty($answeredQuestionId)) continue;
$slackApi='https://slack.com/api/chat.postMessage';
$text="*<@".$message['user_id']."|".$message['username']."> 👉 \"".$answeredQuestion."\" https://nomadlist.com/forum/t/".$answeredQuestionId." *";
echo $text;
if(in_array($text,$alreadySentToSlack)) continue;
array_push($alreadySentToSlack,$text);
$fields=array(
'token'=>$config['slackApi']['token'],
'channel'=>$message['slack_channel_id'],
'text'=>$text,
'as_user'=>false,
'username'=>'Nomad Bot',
'icon_url'=>'https://nomadlist.com/assets/logo.png?'.time()
);
echo "\n\n";
echo json_encode($fields);
echo "\n\n";
// url-ify the data for the POST
$fields_string='';
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
// open connection
$ch = curl_init();
$url=$slackApi;
// set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
// exec
$replyRaw = curl_exec($ch);
echo $replyRaw;
echo "\n\n";
$reply=json_decode($replyRaw,true);
echo json_encode($reply);
echo "\n\n";
}
echo "\n";
exit(90);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment