From b964c061c002918194f601e967b028c7bf415f1f Mon Sep 17 00:00:00 2001 From: Alexander Yakovlev Date: Tue, 23 Jul 2019 08:54:50 +0700 Subject: [PATCH] =?UTF-8?q?=D0=9A=D0=BB=D0=B0=D1=81=D1=81=D1=8B=20=D0=BF?= =?UTF-8?q?=D1=83=D0=B1=D0=BB=D0=B8=D0=BA=D0=B0=D1=86=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Publisher.php | 27 +++++++++++++++ Publisher/Jabber.php | 53 +++++++++++++++++++++++++++++ Publisher/Mastodon.php | 21 ++++++++++++ Publisher/Telegram.php | 23 +++++++++++++ bot.php | 58 +++++--------------------------- composer.json | 4 +-- composer.lock | 76 +++++++++++++++++++++++++++++++++++++++++- config.yml.example | 6 +++- 8 files changed, 215 insertions(+), 53 deletions(-) create mode 100644 Publisher.php create mode 100644 Publisher/Jabber.php create mode 100644 Publisher/Mastodon.php create mode 100644 Publisher/Telegram.php diff --git a/Publisher.php b/Publisher.php new file mode 100644 index 0000000..08d27f9 --- /dev/null +++ b/Publisher.php @@ -0,0 +1,27 @@ +dry_run = $config['DRY_RUN']; + } + + protected function _publish(string $text): void { + } + + public function publish(string $text): void { + if (empty($this->client)) { + return; + } + try { + $this->_publish($text); + } catch (\Exception $e) { + echo $e; + return; + } + if (!$this->dry_run) { + file_put_contents('.lastrun', time()); + } + } +} diff --git a/Publisher/Jabber.php b/Publisher/Jabber.php new file mode 100644 index 0000000..c0c468c --- /dev/null +++ b/Publisher/Jabber.php @@ -0,0 +1,53 @@ +client = new JAXL(array( + 'jid' => $config['JABBER_JID'], + 'pass' => $config['JABBER_PASSWORD'], + )); + $this->client->require_xep([ + '0060' // Publish-Subscribe + ]); + } + } + + protected function _publish(string $text): void { + $this->text = $text; + $this->client->add_cb('on_auth_success', function() { + $this->on_auth_success_callback(); + }); + $client->add_cb('on_auth_failure', function() { + $this->on_auth_failure_callback(); + }); + $this->client->start(); + } + + protected function on_auth_success_callback() + { + // create node + // $client->xeps['0060']->create_node('pubsub.localhost', 'dummy_node'); + // publish + $item = new JAXLXml('item', null, array('id' => time())); + $item->c('entry', 'http://www.w3.org/2005/Atom'); + $item->c('title')->t('Soliloquy')->up(); + $item->c('summary')->t($this->text)->up(); + $item->c( + 'link', + null, + array('rel' => 'alternate', 'type' => 'text/html', 'href' => 'http://denmark.lit/2003/12/13/atom03') + )->up(); + $item->c('published')->t('2003-12-13T18:30:02Z')->up(); + $item->c('updated')->t('2003-12-13T18:30:02Z')->up(); + $this->client->xeps['0060']->publish_item('pubsub.localhost', 'dummy_node', $item); + } + + protected function on_auth_failure_callback($reason) + { + $this->client->send_end_stream(); + } +} diff --git a/Publisher/Mastodon.php b/Publisher/Mastodon.php new file mode 100644 index 0000000..cb650c2 --- /dev/null +++ b/Publisher/Mastodon.php @@ -0,0 +1,21 @@ + 30, + ]); + $this->client = new MastodonClient($guzzle); + $this->client->domain($config['MASTODON_SERVER'])->token($config['MASTODON_ACCESS_TOKEN']); + } + } + + protected function _publish(string $text): void { + $this->client->createStatus($text, [ + 'language' => 'en' + ]); + } +} diff --git a/Publisher/Telegram.php b/Publisher/Telegram.php new file mode 100644 index 0000000..f74a521 --- /dev/null +++ b/Publisher/Telegram.php @@ -0,0 +1,23 @@ +chat_id = $config['TELEGRAM_CHAT_ID']; + $this->client = new Longman\TelegramBot\Telegram( + $config['TELEGRAM_API_KEY'], + $config['TELEGRAM_BOT_NAME'] + ); + } + } + + public function _publish(string $text): void { + \Longman\TelegramBot\Request::sendMessage([ + 'chat_id' => $this->chat_id, + 'text' => $text, + 'parse_mode' => 'Markdown' + ]); + } +} diff --git a/bot.php b/bot.php index a7574ef..aa4ec3a 100755 --- a/bot.php +++ b/bot.php @@ -31,11 +31,16 @@ define('FORMAT',$config['FORMAT']); require_once "Game.php"; require_once "Source.php"; +require_once "Publisher.php"; require_once "_download.php"; $loader = new \Aura\Autoload\Loader; $loader->register(); $loader->addPrefix('Source', 'Source'); +$loader->addPrefix('Publisher', 'Publisher'); +$mastodon = new Publisher\Mastodon($config); +$jabber = new Publisher\Jabber($config); +$telegram = new Publisher\Telegram($config); $parsers = [ // 'urq', @@ -60,25 +65,11 @@ if (file_exists('.lastrun') && !$config['DRY_RUN']) { } $pandoc = new \Pandoc\Pandoc(); -$bitlyProvider = new BitlyProvider( - new GenericAccessTokenAuthenticator($config['BITLY_TOKEN']) -); -if ($config['TELEGRAM'] === true) { - $telegram = new Longman\TelegramBot\Telegram( - $config['TELEGRAM_API_KEY'], - $config['TELEGRAM_BOT_NAME'] - ); -} -if ($config['MASTODON'] === true) { - $guzzle = new GuzzleClient([ - 'timeout' => 30, - ]); - $mastodon = new MastodonClient($guzzle); -} function check($classname, $command) { global $parsers; global $mastodon; + global $jabber; global $telegram; global $config; if (in_array($command, $parsers)) { @@ -94,40 +85,9 @@ function check($classname, $command) { echo $description."\n"; continue; } - if ($config['TELEGRAM'] === true) { - try { - $result = \Longman\TelegramBot\Request::sendMessage([ - 'chat_id' => $config['TELEGRAM_CHAT_ID'], - 'text' => $description, - 'parse_mode' => 'Markdown' - ]); - if (!$config['DRY_RUN']) { - file_put_contents('.lastrun', time()); - } - } catch (Longman\TelegramBot\Exception\TelegramException $e) { - echo $e; - } - } - if ($config['MASTODON'] === true) { - $mastodon->domain($config['MASTODON_SERVER'])->token($config['MASTODON_ACCESS_TOKEN']); - /* - if ($image) { - download('https://ifhub.club'.$image, './'.basename($image)); - $attachment = $mastodon->post('/media', [ - 'file' => file_get_contents('./'.basename($image)) - ]); - var_dump($attachment); - unlink('./'.basename($image)); - $mdescription .= $attachment->url; - } - */ - $mastodon->createStatus($description, [ - 'language' => 'en' - ]); - if (!$config['DRY_RUN']) { - file_put_contents('.lastrun', time()); - } - } + $telegram->publish($description); + $mastodon->publish($description); + $jabber->publish($description); } } } diff --git a/composer.json b/composer.json index 64fc483..d38694c 100644 --- a/composer.json +++ b/composer.json @@ -9,9 +9,9 @@ "cocur/slugify": "^3.1", "ryakad/pandoc-php": "^1.0", "longman/telegram-bot": "dev-master", - "mremi/url-shortener": "dev-master", "revolution/laravel-mastodon-api": "dev-master", - "symfony/yaml": "^4.2@dev" + "symfony/yaml": "^4.2@dev", + "jaxl/jaxl": "^3.1@dev" }, "require-dev": { "phpunit/phpunit": "^7.4@dev" diff --git a/composer.lock b/composer.lock index bbcf8a1..5a68d10 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "content-hash": "c01b375e3fd3d94df22783026d506531", + "content-hash": "831d194251719ea05dde96898c071ffa", "packages": [ { "name": "addwiki/mediawiki-api", @@ -630,6 +630,79 @@ "homepage": "https://laravel.com", "time": "2019-04-11T14:22:55+00:00" }, + { + "name": "jaxl/jaxl", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/jaxl/JAXL.git", + "reference": "f8352008d8550cfc8dbd8f10fd485ca3a98e244a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/jaxl/JAXL/zipball/f8352008d8550cfc8dbd8f10fd485ca3a98e244a", + "reference": "f8352008d8550cfc8dbd8f10fd485ca3a98e244a", + "shasum": "" + }, + "require": { + "ext-curl": "*", + "ext-hash": "*", + "ext-json": "*", + "ext-libxml": "*", + "ext-openssl": "*", + "ext-pcre": "*", + "ext-sockets": "*", + "php": ">=5.2.4" + }, + "require-dev": { + "phpunit/phpunit": "^3.7.0", + "squizlabs/php_codesniffer": "*" + }, + "suggest": { + "ext-pcntl": "Interrupt JAXL with signals" + }, + "bin": [ + "jaxlctl" + ], + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.1.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/JAXL" + ], + "exclude-from-classmap": [ + "/tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Abhinavsingh", + "homepage": "https://abhinavsingh.com/" + } + ], + "description": "Jaxl - Async, Non-Blocking, Event based Networking Library in PHP.", + "homepage": "http://jaxl.readthedocs.org/en/latest/index.html", + "keywords": [ + "abhinavsingh", + "asynchronous", + "event loop", + "http", + "jabber", + "jaxl", + "non blocking", + "php", + "xmpp" + ], + "time": "2018-11-30T18:14:31+00:00" + }, { "name": "longman/telegram-bot", "version": "dev-master", @@ -3305,6 +3378,7 @@ "mremi/url-shortener": 20, "revolution/laravel-mastodon-api": 20, "symfony/yaml": 20, + "jaxl/jaxl": 20, "phpunit/phpunit": 20 }, "prefer-stable": false, diff --git a/config.yml.example b/config.yml.example index 9a6b480..7efb8c4 100644 --- a/config.yml.example +++ b/config.yml.example @@ -1,13 +1,17 @@ DRY_RUN: true TELEGRAM: true # нужен ли Telegram MASTODON: true # нужен ли Mastodon +JABBER: true # нужен ли Jabber + MASTODON_ACCESS_TOKEN: token # токен аккаунта Mastodon MASTODON_SERVER: https://botsin.space MASTODON_USER: user TELEGRAM_API_KEY: some-key-here TELEGRAM_BOT_NAME: bot name TELEGRAM_CHAT_ID: id -BITLY_TOKEN: token # токен сервиса сокращения ссылок bit.ly для Mastodon и Telegram +JABBER_JID: dicebot@oreolek.ru +JABBER_PASSWORD: d20beatsd4butnotd100 + parsers: # delete unused / удалите ненужное - classname: Qsp