Last active
December 5, 2019 23:58
-
-
Save newbold/8b27efc71d54a140273ffc13fb0bb9f7 to your computer and use it in GitHub Desktop.
Keybase bot in PHP
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 | |
// launch with: | |
// $ keybase chat api-listen --filter-channels '[{"name":"channel_name", "topic_name": "channel_name", "members_type": "team"}]' | php -f keybase.php | |
define('KEYBASE_TEAM', 'team_name'); | |
define('KEYBASE_CHANNEL', 'channel_name'); | |
if(!defined('BOT_NAME')) { | |
$keybase_status = `keybase status --json`; | |
define('BOT_NAME', json_decode($keybase_status)->Username); | |
} | |
while(!feof(STDIN)){ | |
$line = fgets(STDIN); | |
$data = json_decode($line); | |
// print_r($data); | |
if(isset($data->msg->at_mention_usernames)) { | |
// Detect bot mentions and respond | |
if(in_array(BOT_NAME, $data->msg->at_mention_usernames)) { | |
$message = $data->msg->content->text->body; | |
$input = str_replace('@'.BOT_NAME.' ', '', $input); | |
$response = 'I heard: '.$message; | |
$command = 'keybase chat send --channel "'.KEYBASE_CHANNEL.'" '.KEYBASE_TEAM.' "'.$response.'"'; | |
$output = shell_exec($command); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment