forked from humanmade/Slackbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.php
124 lines (106 loc) · 2.97 KB
/
plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
/**
* Plugin Name: HM Slack Bot
* Description: Slack off with the bot.
*/
namespace HM\Slack\Bot;
use WP_CLI;
require_once __DIR__ . '/inc/namespace.php';
require_once __DIR__ . '/inc/issues/namespace.php';
require_once __DIR__ . '/vendor/autoload.php';
spl_autoload_register( __NAMESPACE__ . '\\autoload' );
// Force using our fork of phpws
require_once __DIR__ . '/phpws/src/Devristo/Phpws/Client/WebSocket.php';
add_filter( 'hm.slack.bot.message', __NAMESPACE__ . '\\Issues\\parse_issue_message', 10, 2 );
add_filter( 'hm.slack.bot.message', __NAMESPACE__ . '\\Issues\\parse_issue_link', 10, 2 );
// Webhook is disabled in favor of the real bot :)
#add_filter( 'wp_ajax_nopriv_hm_slack_webhook', __NAMESPACE__ . '\\handle_webhook' );
if ( defined( 'WP_CLI' ) && WP_CLI ) {
WP_CLI::add_command( 'hm-slackbot', __NAMESPACE__ . '\\CLI' );
}
/**
* Send convenience action
*
* Rather than always hooking into `message.message` and checking for extra
* pieces, this lets you do the common case.
*/
add_action( 'hm.slack.bot.message.message', function ( $message, $bot ) {
if ( empty( $message->text ) || ! empty( $message->subtype ) ) {
return;
}
do_action( 'hm.slack.bot.message', $message, $bot );
}, 10, 2 );
/**
* Join channels on request
*/
add_action( 'hm.slack.bot.message', function ( $message, $bot ) {
$pattern = '/^' . $bot->get_matchable_name() . ':? join <#(\w+)>/i';
if ( ! preg_match( $pattern, $message->text, $matches ) ) {
return;
}
if ( ! $bot->is_admin( $message->user ) ) {
$bot->send( array(
'type' => 'message',
'channel' => $message->channel,
'text' => 'Sorry, admins only!',
) );
return;
}
$channel = $matches[1];
$bot->send( array(
'type' => 'message',
'channel' => $message->channel,
'text' => "I'd join $channel, but you haven't taught me how yet.",
) );
}, 10, 2 );
/**
* Reload on request
*/
add_action( 'hm.slack.bot.message', function ( $message, $bot ) {
$pattern = '/^' . $bot->get_matchable_name() . ':? reload\b/i';
if ( ! preg_match( $pattern, $message->text ) ) {
return;
}
if ( ! $bot->is_admin( $message->user ) ) {
$bot->send( array(
'type' => 'message',
'channel' => $message->channel,
'text' => 'Sorry, admins only!',
) );
return;
}
$bot->send( array(
'type' => 'message',
'channel' => $message->channel,
'text' => 'Be back in a jiffy.',
) );
$bot->disconnect();
}, 10, 2 );
/**
* Reply to hello!
*
* This is an example of how to use the bot :)
*/
add_action( 'hm.slack.bot.message', function ( $message, $bot ) {
$phrases = array(
'hello',
'hey',
'hi',
'what\'?s up',
'wassup',
'(yo ?)+',
'what\'?s the hiphap',
);
$matcher = '(?:' . implode( '|', $phrases ) . ')';
$name = $bot->get_matchable_name();
$pattern = "/^($matcher $name|$name:? $matcher)[!?]?/i";
if ( ! preg_match( $pattern, $message->text ) ) {
return;
}
$reply = array(
'type' => 'message',
'channel' => $message->channel,
'text' => 'Hello to you too!',
);
$bot->send( $reply );
}, 10, 2 );