Giter Site home page Giter Site logo

Comments (2)

Lukasss93 avatar Lukasss93 commented on June 6, 2024 1

Released Nutgram https://github.com/nutgram/nutgram/releases/tag/4.20.0

You will need to use the $this->getChatId() method if you want to use manual methods (sendMessage, etc...) in the first step of the conversation.

To make a conversation work server-side, see these examples:

Conversation

class BaseConversation extends Conversation
{
    public function start(Nutgram $bot): void
    {
        $bot->sendMessage(
            text: 'What is your name?',
            chat_id: $this->getChatId(), // <--
        );
        $this->next('getName');
    }
    
    public function getName(Nutgram $bot): void
    {
        $name = $bot->message()?->text ?? 'unknown';
        $bot->sendMessage("Hello, $name!");
        $this->end();
    }
}

InlineMenu

class MenuConversation extends InlineMenu
{
    public function start(Nutgram $bot): void
    {
        $bot->sendMessage(
            text: 'Starting...',
            chat_id: $this->getChatId(), // <--
        );
        
        $this->menuText('Message')
            ->addButtonRow(InlineKeyboardButton::make('Say hello', callback_data: "@hello"))
            ->addButtonRow(InlineKeyboardButton::make('Close', callback_data: "@end"))
            ->showMenu();
    }
    
    public function hello(Nutgram $bot): void
    {
        $bot->sendMessage('Hello');
        $this->end();
    }
}

from nutgram.

Lukasss93 avatar Lukasss93 commented on June 6, 2024

Hi, thanks for the feedback.
To make a conversation work server-side, see these examples:

Conversation

class BaseConversation extends Conversation
{
    public function start(Nutgram $bot): void
    {
        //when a server-side conversation is started,
        //the chatId parameter is not automatically filled by Nutgram
        //because we are not in an Update context.
        
        $bot->sendMessage(
            text: 'What is your name?',
            chat_id: $this->getChatId(), //so we need to set the chatId manually (only for this first step)
        );
        
        $this->next('getName');
    }
    
    public function getName(Nutgram $bot): void
    {
        $name = $bot->message()?->text ?? 'unknown';
        
        $bot->sendMessage("Hello, $name!");
        
        $this->end();
    }
}

immagine

InlineMenu

class MenuConversation extends InlineMenu
{
    protected function doOpen(string $text, InlineKeyboardMarkup $buttons, array $opt): Message|null
    {
        //we need to override this method to set the chatId manually
        
        return $this->bot->sendMessage(...[
            'reply_markup' => $buttons,
            'text' => $text,
            'chat_id' => $this->chatId, //required for the first step
            ...$opt,
        ]);
    }
    
    public function start(Nutgram $bot): void
    {
        //when a server-side conversation is started,
        //the chatId parameter is not automatically filled by Nutgram
        //because we are not in an Update context.
        
        $this->chatId = $this->getChatId(); //so we need to set the chatId manually (only for this first step)
        
        $this->menuText('Message')
            ->addButtonRow(InlineKeyboardButton::make('Say hello', callback_data: "@hello"))
            ->addButtonRow(InlineKeyboardButton::make('Close', callback_data: "@end"))
            ->showMenu();
    }
    
    public function hello(Nutgram $bot): void
    {
        $bot->sendMessage('Hello');
        $this->end();
    }
    
    public function closing(Nutgram $bot): void
    {
        $bot->sendMessage('Bye!');
        parent::closing($bot);
    }
}

immagine

Small note

I know it is a bit confusing to make conversations work server-side. We will try to improve it.

from nutgram.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.