Skip to content

Instantly share code, notes, and snippets.

@newbold
Last active December 5, 2019 23:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save newbold/8b27efc71d54a140273ffc13fb0bb9f7 to your computer and use it in GitHub Desktop.
Save newbold/8b27efc71d54a140273ffc13fb0bb9f7 to your computer and use it in GitHub Desktop.
Keybase bot in PHP
<?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