Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
erika committed Jun 18, 2020
0 parents commit 33cb609
Show file tree
Hide file tree
Showing 13 changed files with 472 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
vendor/
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 minicli

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Minichat for Twitch

This project implements a command-line chatbot for Twitch / IRC built with Minicli (PHP).

_Notice: this is not a regular chat client where you can reply from a prompt. You'll have to define automatic responses and behavior programmatically._

## Building Minichat
You can find a detailed tutorial covering this project here:

- [Creating a Twitch / IRC Chatbot in PHP with Minicli](https://dev.to/erikaheidi/creating-a-twitch-irc-chatbot-in-php-with-minicli-45mo)

## Installation & Setup

1. Clone this repository
2. Run `composer install`
3. Set up your Twitch username and [oauth key](https://twitchapps.com/tmi/) in the `minichat` script
4. Run `./minichat twitch` to connect to your stream chat

## TO DO

- Ignore list
- Bot commands
1 change: 1 addition & 0 deletions app/.gitkeep
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.
42 changes: 42 additions & 0 deletions app/Command/Help/DefaultController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace App\Command\Help;

use Minicli\App;
use Minicli\Command\CommandController;

class DefaultController extends CommandController
{
/** @var array */
protected $command_map = [];

public function boot(App $app)
{
parent::boot($app);
$this->command_map = $app->command_registry->getCommandMap();
}

public function handle()
{
$this->getPrinter()->info('Available Commands');

foreach ($this->command_map as $command => $sub) {

$this->getPrinter()->newline();
$this->getPrinter()->out($command, 'info_alt');

if (is_array($sub)) {
foreach ($sub as $subcommand) {
if ($subcommand !== 'default') {
$this->getPrinter()->newline();
$this->getPrinter()->out(sprintf('%s%s','└──', $subcommand));
}
}
}
$this->getPrinter()->newline();
}

$this->getPrinter()->newline();
$this->getPrinter()->newline();
}
}
26 changes: 26 additions & 0 deletions app/Command/Help/TableController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\Command\Help;

use Minicli\Command\CommandController;
use Minicli\Output\Filter\ColorOutputFilter;
use Minicli\Output\Helper\TableHelper;

class TableController extends CommandController
{
public function handle()
{
$this->getPrinter()->display('Testing Tables');

$table = new TableHelper();
$table->addHeader(['Header 1', 'Header 2', 'Header 3']);

for($i = 1; $i <= 10; $i++) {
$table->addRow([$i, rand(0, 10), "other string $i"]);
}

$this->getPrinter()->newline();
$this->getPrinter()->rawOutput($table->getFormattedTable(new ColorOutputFilter()));
$this->getPrinter()->newline();
}
}
16 changes: 16 additions & 0 deletions app/Command/Help/TestController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App\Command\Help;

use Minicli\Command\CommandController;

class TestController extends CommandController
{
public function handle()
{
$name = $this->hasParam('user') ? $this->getParam('user') : 'World';
$this->getPrinter()->display(sprintf("Hello, %s!", $name));

print_r($this->getParams());
}
}
93 changes: 93 additions & 0 deletions app/Command/Twitch/ChatController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace App\Command\Twitch;

use App\TwitchChatClient;
use Minicli\Command\CommandController;

class ChatController extends CommandController
{
public function handle()
{
$this->getPrinter()->info("Starting Minichat...");

$app = $this->getApp();

$twitch_user = $app->config->twitch_user;
$twitch_oauth = $app->config->twitch_oauth;

if (!$twitch_user OR !$twitch_oauth) {
$this->getPrinter()->error("Missing 'twitch_user' and/or 'twitch_oauth' config settings.");
return;
}

$debug_enabled = $app->config->debug;
$client = new TwitchChatClient($twitch_user, $twitch_oauth);

if (!$client->isConnected()) {
$this->getPrinter()->error("It was not possible to connect.");
return;
}

$this->getPrinter()->info("Connected.\n");

while (true) {
$content = $client->read(512);

if ($this->isPing($content)) {
$client->pong();
if ($debug_enabled) {
$this->getPrinter()->out("Pong sent.\n", "dim");
}
continue;
}

if ($this->isMessage($content)) {
$this->printMessage($content);
continue;
}

if ($debug_enabled) {
$this->getPrinter()->out($content . "\n", "dim");
}

sleep(5);
}
}

public function isMessage($content)
{
return strstr($content, 'PRIVMSG');
}

public function isPing($content)
{
return strstr($content, 'PING');
}

public function printMessage($raw_message)
{
$parts = explode(":", $raw_message, 3);
$nick_parts = explode("!", $parts[1]);

$nick = $nick_parts[0];
$message = $parts[2];

$ignore_list = $this->getApp()->config->twitch_chat_ignore;

if (!is_null($ignore_list) AND in_array($nick, $ignore_list)) {
return;
}

$style_nick = "info_alt";

if ($nick === $this->getApp()->config->twitch_user) {
$style_nick = "chat_owner";
}

$this->getPrinter()->out($nick, $style_nick);
$this->getPrinter()->out(': ');
$this->getPrinter()->out($message);
$this->getPrinter()->newline();
}
}
72 changes: 72 additions & 0 deletions app/Command/Twitch/DefaultController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace App\Command\Twitch;

use App\TwitchChatClient;
use Minicli\Command\CommandController;

class DefaultController extends CommandController
{
public function handle()
{
$this->getPrinter()->info("Starting Minichat for Twitch...");

$app = $this->getApp();

$twitch_user = $app->config->twitch_user;
$twitch_oauth = $app->config->twitch_oauth;

if (!$twitch_user OR !$twitch_oauth) {
$this->getPrinter()->error("Missing 'twitch_user' and/or 'twitch_oauth' config settings.");
return;
}

$client = new TwitchChatClient($twitch_user, $twitch_oauth);
$client->connect();

if (!$client->isConnected()) {
$this->getPrinter()->error("It was not possible to connect.");
return;
}

$this->getPrinter()->info("Connected.\n");

while (true) {
$content = $client->read(512);

//is it a ping?
if (strstr($content, 'PING')) {
$client->send('PONG :tmi.twitch.tv');
continue;
}

//is it an actual msg?
if (strstr($content, 'PRIVMSG')) {
$this->printMessage($content);
continue;
}

sleep(5);
}
}

public function printMessage($raw_message)
{
$parts = explode(":", $raw_message, 3);
$nick_parts = explode("!", $parts[1]);

$nick = $nick_parts[0];
$message = $parts[2];

$style_nick = "info";

if ($nick === $this->getApp()->config->twitch_user) {
$style_nick = "info_alt";
}

$this->getPrinter()->out($nick, $style_nick);
$this->getPrinter()->out(': ');
$this->getPrinter()->out($message);
$this->getPrinter()->newline();
}
}
80 changes: 80 additions & 0 deletions app/TwitchChatClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace App;

class TwitchChatClient
{
protected $socket;
protected $nick;
protected $oauth;

static $host = "irc.chat.twitch.tv";
static $port = "6667";
static $pong_response = ":tmi.twitch.tv";

public function __construct($nick, $oauth)
{
$this->nick = $nick;
$this->oauth = $oauth;
}

public function connect()
{
$this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if (socket_connect($this->socket, self::$host, self::$port) === FALSE) {
return null;
}

$this->authenticate();
$this->setNick();
$this->joinChannel($this->nick);
}

public function authenticate()
{
$this->send(sprintf("PASS %s", $this->oauth));
}

public function setNick()
{
$this->send(sprintf("NICK %s", $this->nick));
}

public function joinChannel($channel)
{
$this->send(sprintf("JOIN #%s", $channel));
}

public function getLastError()
{
return socket_last_error($this->socket);
}

public function isConnected()
{
return !is_null($this->socket);
}

public function read($size = 256)
{
if (!$this->isConnected()) {
return null;
}

return socket_read($this->socket, $size);
}

public function send($message)
{
if (!$this->isConnected()) {
return null;
}

return socket_write($this->socket, $message . "\n");
}

public function close()
{
socket_close($this->socket);
}
}
Loading

0 comments on commit 33cb609

Please sign in to comment.