git clone https://github.com/pimax/bitrix24-bot-php-example.git . composer install cp config_example.php config.php
$bot->install(new Bot( $config['alias'], $config['type'], $config['url_message_add'], $config['url_welcome_message'], $config['url_bot_delete'], $config['data'] ));
$bot->uninstall();
$bot->send(new Message("Hello", $_REQUEST['data']['PARAMS']['DIALOG_ID'], [ new Message('[send=/command1]Command 1[/send]'), new Message('[send=/command2]Command 2[/send]'), new Message('[send=/command3]Command 3[/send]'), ]));
switch ($_REQUEST['data']['PARAMS']['MESSAGE']) { case '/command1': $bot->send(new Message("Command 1 response", $_REQUEST['data']['PARAMS']['DIALOG_ID'])); break; case '/command2': $bot->send(new Message("Command 2 response", $_REQUEST['data']['PARAMS']['DIALOG_ID'])); break; case '/command3': $bot->send(new Message("Command 3 response", $_REQUEST['data']['PARAMS']['DIALOG_ID'])); break; default: $bot->send(new Message("Hello", $_REQUEST['data']['PARAMS']['DIALOG_ID'], [ new Message('[send=/command1]Command 1[/send]'), new Message('[send=/command2]Command 2[/send]'), new Message('[send=/command3]Command 3[/send]'), ])); }
git clone https://github.com/pimax/bitrix24-bot-php-example.git . composer install cp config_example.php config.php
'google_token' => '', 'feeds' => [ '/all' => [ 'Title' => 'All jobs', 'Feed' => 'https://job4joy.com/marketplace/rss/' ], '/webdev' => [ 'Title' => 'Web Development', 'Feed' => 'https://job4joy.com/marketplace/rss/?id=3' ], '/software' => [ 'Title' => 'Software Development & IT', 'Feed' => 'https://job4joy.com/marketplace/rss/?id=5' ], '/design' => [ 'Title' => 'Design & Multimedia', 'Feed' => 'https://job4joy.com/marketplace/rss/?id=2' ], '/mobile' => [ 'Title' => 'Mobile Application', 'Feed' => 'https://job4joy.com/marketplace/rss/?id=7' ], '/server' => [ 'Title' => 'Host & Server Management', 'Feed' => 'https://job4joy.com/marketplace/rss/?id=6' ], '/writing' => [ 'Title' => 'Writing', 'Feed' => 'https://job4joy.com/marketplace/rss/?id=8' ], '/customer' => [ 'Title' => 'Customer Service', 'Feed' => 'https://job4joy.com/marketplace/rss/?id=10' ], '/marketing' => [ 'Title' => 'Marketing', 'Feed' => 'https://job4joy.com/marketplace/rss/?id=11' ], '/business' => [ 'Title' => 'Business Services', 'Feed' => 'https://job4joy.com/marketplace/rss/?id=12' ], '/translations' => [ 'Title' => 'Translation & Languages', 'Feed' => 'https://job4joy.com/marketplace/rss/?id=14' ] ]
<?php return [ 'Hello! I can help you with IT projects.' => '! .', 'find work & hire freelancers' => ' ', 'All jobs' => ' ', 'Web Development' => '-', 'Software Development & IT' => ' ', 'Design & Multimedia' => ' ', 'Mobile Application' => ' ', 'Host & Server Management' => ' ', 'Writing' => '', 'Customer Service' => ' ', 'Marketing' => '', 'Business Services' => ' ', 'Translation & Languages' => '', 'New projects not a found!' => ' !' ];
require_once (dirname(__FILE__) .'/GooglShortener.php'); $googl = new GooglShortener($config['google_token']);
/** * Log * * @param mixed $data Data * @param string $title Title * @return bool */ function writeToLog($data, $title = '') { $log = "\n------------------------\n"; $log .= date("Ymd G:i:s") . "\n"; $log .= (strlen($title) > 0 ? $title : 'DEBUG') . "\n"; $log .= print_r($data, 1); $log .= "\n------------------------\n"; file_put_contents(__DIR__ . '/imbot.log', $log, FILE_APPEND); return true; }
/** * Send Help Message * * @param $bot Bot instance * @return bool */ function sendHelpMessage($bot) { global $config; $attach = []; foreach ($config['feeds'] as $com => $feed) { $attach[] = new Message('[send='.$com.']'.$bot->t($feed['Title']).'[/send]'); } $bot->send(new Message($bot->t('Hello! I can help you with IT projects.'), $_REQUEST['data']['PARAMS']['DIALOG_ID'], $attach)); return true; }
/** * Get Feed Data * * @param $feed Feed data * @param $bot Bot instance * @return bool */ function getFeed($feed, $bot) { global $googl; try { $reader = new Reader; $resource = $reader->download($feed['Feed']); $parser = $reader->getParser( $resource->getUrl(), $resource->getContent(), $resource->getEncoding() ); $feed = $parser->execute(); $items = array_reverse($feed->getItems()); if (count($items)) { foreach ($items as $itm) { $url = $googl->shorten($itm->getUrl()); $message = substr(strip_tags($itm->getContent()), 0, 150); $bot->send(new Message("[B]".$itm->getTitle() . "[/B]\n" . $message . "\n", $_REQUEST['data']['PARAMS']['DIALOG_ID']), [ new Link($url->id, $url->id) ]); } } else { $bot->send(new Message($bot->t('New projects not a found!'), $_REQUEST['data']['PARAMS']['DIALOG_ID'])); } } catch (Exception $e) { writeToLog($e->getMessage(), 'Exception'); } return true; }
Source: https://habr.com/ru/post/282077/
All Articles