php-telegram-bot / core

PHP Telegram Bot based on the official Telegram Bot API
MIT License
3.88k stars 954 forks source link

walk through images of a product in a telegram bot #764

Closed ahmadbadpey closed 4 years ago

ahmadbadpey commented 6 years ago

I'm working on a telegram bot shop via https://github.com/php-telegram-bot/core in laravel.

In this app , each Product has many images that path of them is stored on th DB.

Now I want when send details of a product , images of that shown one by one that user can navigate them via a prev and next inline keyboard. like this picture :

enter image description here

For that after show all products in the shop as a inline query and after use choose one of them , On Chosen inline result Command, I get a product Id and fetch first image of that from DB like this :

class ChoseninlineresultCommand extends SystemCommand
    {
    public function execute ()
    {
        $chosenInlineResult   = $this->getChosenInlineResult();
        $chosenInlineResultId = $product_id = $chosenInlineResult->getResultId();
        $chat_id              = $chosenInlineResult->getFrom()->getId();

        $product = Product::findOrFail($product_id);

        $picture = $product->images->first();

        $keyboard = KeyboardController::showProductKeyboard($product_id, $chat_id);

        $result = view('show-product', compact('product','picture'))->render();

        Request::sendMessage([
            'chat_id'      => $chat_id,
            'text'         => $result,
            'reply_markup' => $keyboard,
            'parse_mode'   => 'HTML'
        ]);
    }
    }

Also showProductKeyboard of KeyboardController is like this :

    static public function showProductKeyboard ($product_id, $user_id)
    {

        $inlineKeyboard = new InlineKeyboard([]);
        $inlineKeyboard->addRow(new InlineKeyboardButton([
            'text'          => ' previous picture ⬅️️',
            'callback_data' => 'prev_pic'
        ]), new InlineKeyboardButton([
            'text'          => '➡️ next picture ',
            'callback_data' => 'next_pic'
        ]));

        return $inlineKeyboard;
    }

And finally show-product blade is simple as :

🛍️ <b>{{$product->title}}</b>

✅ <b>{{str_limit(strip_tags($product->desc), 50)}}</b>

💵 <b>{{number_format($product->price)}}</b> dollor

<a href="{{config('app.url').'/upload/'. $picture}}"> Picture</a>

Problem is that I do not know how can I implement what I want .

noplanman commented 6 years ago

@ahmadbadpey Sorry for the late reply to this...

Easiest would be to pass the page / photo ID together with the callback_data, then you can edit the message and update the URL to the next / previous image respectively.

akalongman commented 6 years ago

@ahmadbadpey did you solved your problem?