Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace AppHttpControllers;
- use TelegramBotLaravelFacadesTelegram;
- class TelegramController extends Controller
- {
- public function index()
- {
- $telegram = new Api(config('telegram.bot_token'));
- dd($response = $telegram->setWebhook([
- 'url' => 'https://telebot.dev/<token>',
- 'certificate' => '/my.pem'
- ]));
- }
- }
- <?php
- namespace TelegramBotCommands;
- /**
- * Class HelpCommand.
- */
- class HelpCommand extends Command
- {
- /**
- * @var string Command Name
- */
- protected $name = 'help';
- /**
- * @var string Command Description
- */
- protected $description = 'Help command, Get a list of commands';
- /**
- * {@inheritdoc}
- */
- public function handle($arguments)
- {
- $commands = $this->telegram->getCommands();
- $text = '';
- foreach ($commands as $name => $handler) {
- $text .= sprintf('/%s - %s'.PHP_EOL, $name, $handler->getDescription());
- }
- $this->replyWithMessage(compact('text'));
- }
- }
- $updates = Telegram::getUpdates(); // array of Update
- // Бот отвечает сообщением, которое получил добавляя префикс "echo: "
- public function webHookAction()
- {
- if (config('app.env') == 'local') {
- $updates = Telegram::getUpdates();
- } else {
- $updates = [Telegram::getWebhookUpdates()];
- }
- /** @var Update[] $updates */
- foreach ($updates as $update) {
- $fromChatId = $update->getMessage()->getChat()->getId();
- $receivedMessage = $update->getMessage()->getText();
- Telegram::sendMessage([
- 'chat_id' => $fromChatId,
- 'parse_mode' => 'HTML',
- 'text' => 'echo: <i>' . $receivedMessage . "</i>",
- ]);
- }
- }
- Route::post('/' . config('telegram.bot_token'), function (Request $request) {
- $update = Telegram::commandsHandler(true);
- return 'ok';
- });
- class StartCommand extends Command {
- protected $name = "start";
- /**
- * @var string Command Description
- */
- protected $description = "Команда для начала общения с ботом";
- /**
- * @inheritdoc
- */
- public function handle($arguments)
- {
- $this->replyWithMessage(['text' => 'Привет я Бот, вот список команд которые вам доступны:']);
- // Посылаем статус что печатаем
- $this->replyWithChatAction(['action' => Actions::TYPING]);
- $commands = $this->getTelegram()->getCommands();
- // Строим список команд
- $response = '';
- foreach ($commands as $name => $command) {
- $response .= sprintf('/%s - %s' . PHP_EOL, $name, $command->getDescription());
- }
- //Посылаем ответ
- $this->replyWithMessage(['text' => $response]);
- }
- }
Add Comment
Please, Sign In to add comment