@nikitazapevalov

Как получить переменную из внешней функции во внутренней?

Как из $bot->command получить значение массива $pr?Вне index() ничего писать нельзя:
<?php

class ControllerApiTelegram extends Controller {
   
	public function index() {
	 $this->load->model('catalog/product');
    $pr = $this->model_catalog_product->getProduct(430);
	//$this->log->write(print_r($pr,true));
	try {
		$bot = new \TelegramBot\Api\Client('875746922:AAFpVCA-KCaInxfWKesNCs-bElBb7i8ZfLc');
		
		$bot->command('devanswer', function ($message) use ($bot) {
			preg_match_all('/{"text":"(.*?)",/s', file_get_contents('http://devanswers.ru/'), $result);
			$bot->sendMessage($message->getChat()->getId(),
				str_replace("<br/>", "\n", json_decode('"' . $result[1][0] . '"')));
		});
		
		$bot->command('qaanswer', function ($message) use ($bot) {
            global $arr
             $this->log->write(print_r("тетс",true));
			$bot->sendMessage($message->getChat()->getId(), str_replace("<br/>", "\n", $arr["1"]));
            $bot->sendMessage($message->getChat()->getId(), str_replace("<br/>", "\n", "тестовый текст"));
		});
		$bot->run();
	} catch (\TelegramBot\Api\Exception $e) {
		$e->getMessage();
	}
}
}
  • Вопрос задан
  • 60 просмотров
Решения вопроса 1
iiifx
@iiifx
PHP, OOP, SOLID, Yii2, Composer, PHPStorm
$bot->command('devanswer', function ($message) use ($bot, $pr) {

Оно?
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы