<?php use NielsBot\Plugins\CommandEvent; use NielsBot\Plugins\MessageEvent; use NielsBot\Plugins\Plugin; $repeatMe = new Plugin('RepeatMe', 'Annoying plugin repeating certain messages.'); $repeatMe->on('message', function (MessageEvent $event) { $message = $event->getMessage(); $emoticons = [':)', ':c', 'c:', ':(', ':\')', ':")', ':\'(', ':d', ':s', ';)', ':3', ':u', ':o', '( ͡° ͜ʖ ͡°)', ':-)', ':-(']; if (in_array(strtolower($message), $emoticons) || rand(0, 10000) < 10) { $event->getChat()->sendMessage($message); } if (strtolower($message) == 'xd') { $response = rand(0, 1) == 1 ? 'x' : 'X'; for ($i = 0; $i < rand(1, 7); $i++) { $response .= rand(0, 1) == 1 ? 'd' : 'D'; } $event->getChat()->sendMessage($response); } }); $repeatMe->on('command', ['repeat', 'echo'], function (CommandEvent $event) { $command = $event->getCommand(); $message = $event->getMessage(); $event->getChat()->sendMessage($message); });
<?php use NielsBot\Core\Chat; use NielsBot\Plugins\CommandEvent; use NielsBot\Plugins\PhotoEvent; use NielsBot\Plugins\Plugin; use NielsBot\Plugins\ReplyEvent; use NielsBot\Telegram\PhotoSize; use NielsBot\Telegram\TelegramChat; $photoUtils = new Plugin('Photo Utils', 'Photo utilities plugin.'); if (!file_exists(__DIR__ . '/../tmp/')) { mkdir(__DIR__ . '/../tmp/'); } $photoUtils->on('command', ['photo', 'photoutils', 'photoeffects'], function (CommandEvent $event) { $message = '*Photo utils*' . PHP_EOL; $message .= 'To use this plugin, send a photo with one or more of the following options in the caption:' . PHP_EOL . PHP_EOL; $message .= '`face` - Zoom in on the face' . PHP_EOL; $message .= '`googlify` - Add googly eyes' . PHP_EOL; $message .= '`nose` - Add a new nose' . PHP_EOL; $message .= '`kiss` - This person likes to kiss' . PHP_EOL; $message .= '`jpeg` - Need more JPEG' . PHP_EOL; $message .= '`debug` - Sends some debugging info' . PHP_EOL; $event->getChat()->sendMessage($message); }); $photoUtils->on('photo', function (PhotoEvent $event) { respond_to_photo($event->getChat(), $event->getCaption(), $event->getSizes()); }); $photoUtils->on('reply', ['photo'], function (ReplyEvent $event) { respond_to_photo($event->getChat(), $event->getMessage(), $event->getParent()->getSizes()); }); /**
<?php use NielsBot\Plugins\CommandEvent; use NielsBot\Plugins\Plugin; $lenny = new Plugin('Lenny', 'Can u handle the dank me-mays m8?'); $lenny->on('command', ['lenny'], function (CommandEvent $event) { switch ($event->getMessage()) { case 'hugs': case 'hug': $lenny = '(つ ͡° ͜ʖ ͡°)つ'; break; case 'no': $lenny = '( ͡°_ʖ ͡°)'; break; case 'lenninati': $lenny = '( ͡∆ ͜ʖ ͡∆)'; break; case 'backward': case 'backwards': $lenny = '( °͡ ʖ͜ °͡ )'; break; case 'pumped': $lenny = '(ง ͠° ͟ل͜ ͡°)ง'; break; case 'list': $lenny = 'hug(s), no, lenninati, backward(s), pumped, list, default'; break; default: $lenny = '( ͡° ͜ʖ ͡°)'; break; }
<?php use NielsBot\Plugins\AudioEvent; use NielsBot\Plugins\CommandEvent; use NielsBot\Plugins\Plugin; use NielsBot\Telegram\TelegramChat; $audioUtilsPlugin = new Plugin('Audio Utilities', 'Provides utilities for audio'); $audioUtilsPlugin->on('audio', null, function (AudioEvent $event) { return; // disable function because it does not work properly $event->getChat()->sendAction(TelegramChat::TYPING); $ch = curl_init(); $data = http_build_query(['api_key' => file_get_contents(__DIR__ . '/../ECHONEST_TOKEN'), 'url' => $event->getPath()]); curl_setopt($ch, CURLOPT_URL, 'http://developer.echonest.com/api/v4/track/upload'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); $content = curl_exec($ch); $json = json_decode($content, true); $message = 'Unknown track'; if ($json['response']['track']['audio_md5'] !== null) { $message = $json['response']['track']['artist'] . ' - ' . $json['response']['track']['title']; } $event->getChat()->sendMessage($message); }); $audioUtilsPlugin->on('command', ['speak', 'speech', 'voice'], function (CommandEvent $event) { $languages = ['ca-es', 'zh-cn', 'zh-hk', 'zh-tw', 'da-dk', 'nl-nl', 'en-au', 'en-ca', 'en-gb', 'en-in', 'en-us', 'fi-fi', 'fr-ca', 'fr-fr', 'de-de', 'it-it', 'ja-jp', 'ko-kr', 'nb-no', 'pl-pl', 'pt-br', 'pt-pt', 'ru-ru', 'es-mx', 'es-es', 'sv-se']; $message = $event->getMessage(); $lang = 'en-us';
<?php use NielsBot\Core\NielsBot; use NielsBot\Plugins\Plugin; use NielsBot\Plugins\CommandEvent; $commonPlugin = new Plugin('Common', 'Basic functions for bot'); $commonPlugin->on('command', ['plugins'], function (CommandEvent $event) { $message = '*Plugin list*' . PHP_EOL . PHP_EOL; foreach (NielsBot::getInstance()->getPlugins() as $plugin) { $message .= '*' . $plugin->getName() . '*' . PHP_EOL; $message .= $plugin->getDescription() . PHP_EOL; $message .= PHP_EOL; } $event->getChat()->sendMessage($message); }); $commonPlugin->on('command', ['help', 'commands'], function (CommandEvent $event) { $message = '*Command list*' . PHP_EOL . PHP_EOL; foreach (NielsBot::getInstance()->getPlugins() as $plugin) { $message .= '*' . $plugin->getName() . '*' . PHP_EOL; $i = 0; foreach ($plugin->getEvents() as $type => $commands) { if ($type === 'command') { foreach ($commands as $command => $exec) { $i++; $message .= '/' . $command . PHP_EOL; } } } if ($i == 0) { $message .= '_No commands_' . PHP_EOL; }
<?php use NielsBot\Plugins\MessageEvent; use NielsBot\Plugins\Plugin; $gitHub = new Plugin('GitHub', 'Posts info about GitHub repositories.'); $gitHub->on('message', function (MessageEvent $event) { // TODO store github token in some kind of database? if (!file_exists(__DIR__ . '/../GITHUB_TOKEN')) { return; } $message = $event->getMessage(); if (!preg_match_all('/(?:.*)https?:\\/\\/github\\.com\\/([a-zA-Z0-9-_\\.]+)\\/?([a-zA-Z0-9-_\\.]+)?\\/?([a-zA-Z0-9-_\\/\\.]+)?(?:.*)/', $message, $matches)) { return; } $user = $matches[1][0]; $repo = $matches[2][0] ?? null; $ch = curl_init('https://api.github.com/repos/' . urlencode($user) . '/' . urlencode($repo)); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_USERAGENT, 'NielsBot'); curl_setopt($ch, CURLOPT_USERPWD, file_get_contents(__DIR__ . '/../GITHUB_TOKEN')); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $apiResult = curl_exec($ch); curl_close($ch); if ($apiResult === false) { $event->getChat()->sendMessage('Could not read from GitHub repository.' . PHP_EOL . PHP_EOL . $apiResult); return; } $apiResult = json_decode($apiResult, true); $message = '*' . $apiResult['full_name'] . '*' . PHP_EOL; $message .= $apiResult['description'] . PHP_EOL;
<?php use NielsBot\Plugins\CommandEvent; use NielsBot\Plugins\Plugin; $rtdPlugin = new Plugin('Roll the Dice', 'Roll the Dice plugin'); $rtdPlugin->on('command', ['rtd'], function (CommandEvent $event) { $result = rand(1, 6); $event->getChat()->sendMessage('Rolled number ' . $result); });