PHP code example of 3neti / messaging-bot

1. Go to this page and download the library: Download 3neti/messaging-bot library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

3neti / messaging-bot example snippets


use LBHurtado\MessagingBot\Drivers\Telegram\TelegramDriver;

$driver = app(TelegramDriver::class);
$driver->setWebhook('https://yourdomain.com/messaging/telegram/webhook');

namespace LBHurtado\MessagingBot\Handlers;

use LBHurtado\MessagingBot\Data\NormalizedResponse;
use LBHurtado\MessagingBot\Data\NormalizedUpdate;

class MyHandler extends BaseMessagingHandler
{
    protected function process(NormalizedUpdate $update): NormalizedResponse
    {
        return NormalizedResponse::text('Hello from MyHandler!');
    }
    
    // Optional: 

// config/messaging-bot.php
'handlers' => [
    'my_intent' => \LBHurtado\MessagingBot\Handlers\MyHandler::class,
],

namespace LBHurtado\MessagingBot\Flows;

class MyFlow extends BaseFlow
{
    public function initialStep(): string
    {
        return 'step1';
    }
    
    public function steps(): array
    {
        return ['step1', 'step2', 'finalize'];
    }
    
    protected function promptStep1(ConversationState $state): NormalizedResponse
    {
        return NormalizedResponse::text('Enter something:');
    }
    
    protected function handleStep1(NormalizedUpdate $update, ConversationState $state, string $input): array
    {
        $newState = $state->with('value', $input)->advanceTo('step2');
        
        return [
            'response' => $this->promptStep2($newState),
            'state' => $newState,
        ];
    }
    
    // ... more steps
}
bash
php artisan vendor:publish --tag=messaging-bot-config
bash
# Simulate a message
php artisan test:messaging "/redeem"

# Continue the conversation
php artisan test:messaging "VOUCHER-CODE"

# Simulate contact sharing (phone number)
php artisan test:messaging "" --contact

# Test deep link (as if user clicked t.me/bot?start=redeem_CODE)
php artisan test:messaging "/start redeem_ABCD"

# Use different chat ID (for separate conversation state)
php artisan test:messaging "/redeem" --chat-id=99999

# Specify mobile number for contact sharing
php artisan test:messaging "" --contact --mobile=09181234567
bash
php artisan messaging:poll
bash
# Test the full redemption flow
php artisan cache:clear
php artisan test:messaging "/start redeem_VOUCHER-CODE"
php artisan test:messaging "" --contact
php artisan test:messaging "accept"

# Test returning user (with cached phone)
php artisan tinker --execute="Cache::put('messaging:phone:12345', '+639173011987', now()->addDays(30));"
php artisan test:messaging "/start redeem_ANOTHER-CODE"
php artisan test:messaging "accept"