laravel telegram bot handel nested menu response

  keyboard, laravel, telegram, telegram-bot

Recently, I’ve been trying to design a RESTful API as a backend for the mobile app and other platform.Now I need to design a telegram bot As an internal package.
All telegram responses send to the specific URL like this: https://myurl.com/bot….
I have a nested menu(keyboard) and should manage every menu in the main controller.
enter image description here
enter image description here
enter image description here

use Cache;
use TelegramBotApi;
use TelegramBotKeyboardKeyboard;

class TelegramController
{
   // protected $mainKey = collect();
   protected $telegram  ;
   protected $keyboard  ;

   public function __construct()
   {
       $this->telegram = new Api('my token');
       $this->keyboard = Keyboard::make();
   }

   public function setWebhook()
   {
       return $this->telegram->setWebhook([
           'url' => str_replace('http', 'https', url(route('get.update')))
       ]);
   }

   public function getWebhookInfo()
   {
       return $this->telegram->getWebhookInfo();
   }

   public function getUpdate()
   {
       $message = $this->telegram->getWebhookUpdate()->getMessage();

       $id = $message['from']['id'];
       $text = trim('/', $message['text']);

       $state = explode('|', Cache::store('redis_i')->get($id . '_state'));
       $lastState = collect($state)->last();
       $firsState = collect($state)->first();

       if ($firsState == $lastState || isEmpty($state))
       {
           switch ($state)
           {
               case 'store':
                   $Keyboard = [
                       ['product', 'report', 'category'],
                   ];
                   $textMessage = 'store';
                   // save user state for show relented sub menu
                   Cache::store('redis_i')->put($id . '_state', 'store');
                   break;
               case 'Support':
                   $Keyboard = [
                       ['technical', 'sell'],
                   ];
                   $textMessage = 'Support';
                   Cache::store('redis_i')->put($id . '_state', 'Support');
                   // no break
               default:
               $Keyboard = [
                   ['stores'],
                   ['Support'],
               ];
               $textMessage = "command doesn't exist";
                   break;
           }
       }
       // show relented sub menu
       // can be to many sub-menu
       else
       {  
           switch ($state)
           {
               case 'store/product':
                   $Keyboard = [
                       ['add product', 'edit product'],
                       ['delete product', 'show all product'],
                   ];
                   $textMessage = 'store';
                   Cache::store('redis_i')->put($id . '_state', 'store/product');
                   break;
               case 'store/category':
                   $Keyboard = [
                       ['add category', 'edit category'],
                       ['delete category', 'show all category'],
                   ];
                   $textMessage = 'Support';
                   Cache::store('redis_i')->put($id . '_state', 'store/category');
              .
              .
              .
              .
           }
           
       }
       // other conditions for delete,edit, ...
       ................

       $reply_markup = Keyboard::make([
           'keyboard' => $Keyboard,
           'resize_keyboard' => true,
           'one_time_keyboard' => true
       ]);
       if ($lastState == 'product')
       {
           $Keyboard = [
               ['product add', 'product delete', 'product update'],
           ];
       }
       $response = $this->telegram->sendMessage([
           'chat_id' => $id,
           'text' => $textMessage,
           'reply_markup' => $reply_markup
       ]);

       $messageId = $response->getMessageId();
   }
}

I have two problems:

1- How to call the internal API and get the results?

2-How can I manage each section separately in a class?

Something like apiResource in Laravel :

class ProductController
{
   protected $telegram  ;
   protected $keyboard  ;
   protected $mainKey  ;

   public function __construct(Api $telegram)
   {
       $this->telegram = $telegram;
       $this->keyboard = Keyboard::make();
   }

   public function index()
   {

   }

   public function create()
   {

   }

   public function destroy()
   {

   }
   .
   .
   .   
}

use the laravel and telegram-bot-sdk

Source: Laravel

Leave a Reply