Skip to content

Instantly share code, notes, and snippets.

@madfriend
Created August 22, 2012 20:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save madfriend/3429018 to your computer and use it in GitHub Desktop.
Save madfriend/3429018 to your computer and use it in GitHub Desktop.
Bot API example
<?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