示例#1
0
 /**
  * Respond to mentions with a message.
  *
  * @param  DanGreaves\SlackBot\Entities\Message $message
  * @return void
  */
 public function react(Message $message)
 {
     // Fetch reaction from config
     $reaction = $this->config('message', 'Bleep. Bloop. Hey :username, I\'m alive but I\'m a pretty dull fellow at the mo. You can contribute to my commands at http://github.com/dangreaves/slackbot ⚡');
     // Add username to message
     $reaction = str_replace(':username', $message->getUser()->getFirstName() ?: $message->getUser()->getUsername(), $reaction);
     // Reply with the message
     $message->reply($reaction);
     // Update the last sent timestamp
     $this->lastMessageSentAt = Carbon::now();
 }
示例#2
0
 /**
  * Run the bot process.
  *
  * @return void
  */
 public function run()
 {
     $loop = \React\EventLoop\Factory::create();
     $client = new RealTimeClient($loop);
     $client->setToken($this->token);
     // Respond to RTM messages
     $client->on('message', function ($payload) use($client) {
         // Output debug
         $this->debug('Message received', $payload->getData());
         // Create a message entity from payload
         $message = new Message($client, $payload);
         // Certain message types are blocked
         if (!$message->isValidForReaction()) {
             return;
         }
         // Execute relevant command
         $this->react($message);
     });
     $this->debug('Connecting to RTM socket');
     $client->connect()->then(function () {
         $this->debug('Connected to RTM socket');
     });
     $loop->run();
 }