Created
August 22, 2012 20:19
-
-
Save madfriend/3429018 to your computer and use it in GitHub Desktop.
Bot API example
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 | |
// A short example of my current bot API usage | |
hear("/^close( issue)? #(?P<id>\d+)/iu", function($bot, $msg, $matches) { | |
// This bot likes to work with Redmine | |
$issue = new \Redmine\Issue(); | |
$issue = $issue->find($matches['id']); | |
// Is issue present? | |
if ($issue->error) { | |
$msg->body = "Issue #{$matches['id']} was not found! Sorry."; | |
} | |
else { | |
// Ask for confirmation | |
$msg->body = "Did you mean \"{$issue->subject}\"?"; | |
// Save this person' jabber id | |
$jid = $msg->to; | |
// Add another callback | |
hear($pattern = "/^yes/iu", function($bot, $msg, $matches) use ($jid, $pattern, $issue) { | |
// Was it the same person? | |
// TODO: separated pattern-callback pairs for different users | |
if ($msg->to !== $jid) return; | |
// close issue | |
$issue->set('status_id', \Redmine\Issue::STATUS_RESOLVED); | |
$issue->save(); | |
// Reply | |
$msg->body = "Done."; | |
$bot->send($msg); | |
// Forget about this action | |
forget($pattern); | |
}); | |
} | |
$bot->send($msg); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment