PHP Classes

File: Resources/Privat/Scripts/botty.php

Recommend this page to a friend!
  Classes of FN   PHP Typo3 Slack Channel Bot   Resources/Privat/Scripts/botty.php   Download  
File: Resources/Privat/Scripts/botty.php
Role: Example script
Content type: text/plain
Description: Example script
Class: PHP Typo3 Slack Channel Bot
Chat bot to interact in Typo3 Slack channel
Author: By
Last change:
Date: 5 years ago
Size: 3,632 bytes
 

Contents

Class file image Download
<?php
/**
 * T3Bot.
 *
 * @author Frank Nägler <frank.naegler@typo3.org>
 *
 * @link http://www.t3bot.de
 * @link http://wiki.typo3.org/T3Bot
 */

use Doctrine\DBAL\Configuration;
use
Doctrine\DBAL\DriverManager;
use
Slack\Message\Attachment;
use
Slack\Message\Message;
use
T3Bot\Commands\AbstractCommand;
use
T3Bot\Commands\ChannelCommand;
use
T3Bot\Commands\TellCommand;
use
T3Bot\Slack\CommandResolver;

require_once
__DIR__.'/../../../vendor/autoload.php';
require_once
__DIR__.'/../../../config/config.php';

// The Slack connection
$loop = React\EventLoop\Factory::create();

$client = new Slack\RealTimeClient($loop);
$client->setToken($GLOBALS['config']['slack']['botAuthToken']);

$client->on('message', function (Slack\Payload $payload) use ($client) {
   
$user = $payload->getData()['user'];
   
$blackList = array_map('trim', explode(',', $GLOBALS['config']['slack']['userBlacklist']));
    if (
in_array($user, $blackList, true)) {
       
$client->apiCall('im.open', ['user' => $user])
            ->
then(function (Slack\Payload $response) use ($client) {
               
$message = new Message($client, [
                   
'unfurl_links' => false,
                   
'unfurl_media' => false,
                   
'parse' => 'none',
                   
'text' => 'Sorry, but you are blacklisted!',
                   
'channel' => $response->getData()['channel']['id']
                ]);
               
$client->postMessage($message);
            });
    } else {
        if (
$payload->getData()['user'] !== $GLOBALS['config']['slack']['botId']) {
           
$commandResolver = new CommandResolver($payload, $client);
           
$command = $commandResolver->resolveCommand($GLOBALS['config']);
            if (
$command instanceof AbstractCommand) {
               
$result = $command->process();
                if (
$result !== false) {
                   
$command->sendResponse($result);
                } else {
                   
$command->sendResponse($command->getHelp());
                }
            }
        }
    }
});

$client->on('channel_created', function (Slack\Payload $payload) use ($client) {
    if (
$payload->getData()['user'] !== $GLOBALS['config']['slack']['botId']) {
       
$command = new ChannelCommand($payload, $client, $GLOBALS['config']);
       
$command->processChannelCreated($payload->getData());
    }
});

$client->on('presence_change', function (Slack\Payload $payload) use ($client) {
    if (
$payload->getData()['user'] !== $GLOBALS['config']['slack']['botId']) {
       
$command = new TellCommand($payload, $client, $GLOBALS['config']);
       
$command->processPresenceChange($payload->getData()['user'], $payload->getData()['presence']);
    }
});

$client->connect()->then(function () {
    echo
"Connected!\n";
});

$db = DriverManager::getConnection($GLOBALS['config']['db'], new Configuration());

$loop->addPeriodicTimer(5, function () use ($client, $db) {
   
$messages = $db->createQueryBuilder()
        ->
select('*')
        ->
from('messages')
        ->
orderBy('id', 'ASC')
        ->
execute()
        ->
fetchAll();
    foreach (
$messages as $message) {
       
$data = json_decode($message['message'], true);
       
$attachments = $data['attachments'];
       
$data['attachments'] = [];
        foreach (
$attachments as $attachment) {
           
$data['attachments'][] = Attachment::fromData($attachment);
        }
       
// process data
       
$messageToSent = new Message($client, $data);
       
$client->postMessage($messageToSent);

       
// delete message
       
$db->delete('messages', ['id' => $message['id']]);
    }
});

$loop->run();