public function testFileQueue() { $dir = ONPHP_TEMP_PATH . 'tests/messages'; $uri = $dir . '/fileQueueItems'; if (!is_dir($dir)) { mkdir($dir, 0700, true); } if (file_exists($uri)) { unlink($uri); } $queue = TextFileQueue::create()->setFileName($uri); $sender = TextFileSender::create()->setQueue($queue); $receiver = TextFileReceiver::create()->setQueue($queue); $sender->send(TextMessage::create()->setText('first ape')); $sender->send(TextMessage::create()->setText('second ape')); $message = $receiver->receive(); $this->assertNotNull($message); $this->assertEquals('first ape', $message->getText()); $message = $receiver->receive(); $this->assertNotNull($message); $this->assertEquals('second ape', $message->getText()); $sender->send(TextMessage::create()->setText('third ape')); $message = $receiver->receive(); $this->assertNotNull($message); $this->assertEquals('third ape', $message->getText()); }
/** * Фабрикует класс, обрабатывающий сообщение * @param array $m Сообщение на вход * @return object Класс, ответственный за данный тип сообщения */ public static function parseInput($m) { if (empty($m['message_id'])) { return NULL; } if (!empty($m['text'])) { $command = Command::create($m); return $command ? $command : TextMessage::create($m); } elseif (!empty($m['location'])) { return Location::create($m); } }
public function say($text, $reply_to = null, array $reply_markup = null, array $additionalParams = []) { if ($this->parse_mode && empty($additionalParams['parse_mode'])) { $additionalParams['parse_mode'] = $this->parse_mode; } if (!$reply_markup && $this->hide_keyboard) { $reply_markup = ['hide_keyboard' => true]; } $msg = $this->api->sendMessage($this->msg->getChat()->getID(), $text, $reply_to, $reply_markup, $additionalParams); if (empty($msg['ok'])) { throw new \Exception("Error Processing Request: " . print_r($msg, true)); } return TextMessage::create($msg['result']); }
/** * @return Message **/ public function receive($uTimeout = null) { if (!$this->queue) { throw new WrongStateException('you must set the queue first'); } if ($uTimeout && $this->getStream()->isEof()) { usleep($uTimeout); } $string = $this->getStream()->readString(); if (!$string && $this->getStream()->isEof()) { return null; } $this->getQueue()->setOffset($this->getStream()->getOffset()); $string = rtrim($string, PHP_EOL); $chunks = preg_split("/\t/", $string, 2); $time = isset($chunks[0]) ? $chunks[0] : null; $text = isset($chunks[1]) ? $chunks[1] : null; Assert::isNotNull($time); $result = TextMessage::create(Timestamp::create($time))->setText($text); return $result; }