Search by

MAX Messenger SDK Bot API PHP


README

API MAX Project Status

Laravel versions Laravel Latest Version on Packagist PHP version Total Downloads GitHub code size in bytes Software License

Functionality

violetsun/max is a Laravel package for integration with the official MAX API.

The package provides a convenient way to work with MAX bots and chats directly from a Laravel application: sending messages, uploading files, working with attachments, handling chats and users, and using a fluent message builder.

Main features

  • Integration with the official MAX API.
  • Laravel facade for simple API calls.
  • Sending text messages to chats and users.
  • Editing and deleting messages.
  • Fluent MessageBuilder for building complex messages.
  • Support for message attachments:
    • images;
    • videos;
    • audio;
    • files;
    • stickers;
    • contacts;
    • locations;
    • shared links;
    • inline keyboards.
  • Uploading local files to MAX and using returned upload tokens.
  • Inline keyboard builder with support for:
    • callback buttons;
    • link buttons;
    • geolocation request buttons;
    • contact request buttons;
    • open app buttons;
    • message buttons;
    • clipboard buttons.
  • Eloquent models for storing MAX users and chats.
  • Many-to-many relation between MAX chats and MAX users.
  • Optional saving of update data in the application database.
  • Optional queue support for processing updates.
  • Publishable config, migrations, models, assets and services.

Laravel integration

The package registers a Laravel service provider and exposes the MAX API client through the MAX facade:

Installation

Installing root certificates for working with the MAX API2

  • Download root certificates https://www.gosuslugi.ru/crt
  • Install certificates sudo cp CERTIFICATE_NAME.crt /usr/local/share/ca-certificates/
  • Update the certificate store sudo update-ca-certificates

Via Composer

composer require violetsun/max

Package vendor publish

php artisan vendor:publish --provider="VioletSun\MAX\MAXServiceProvider"

or

php artisan vendor:publish --tag=max-config
php artisan vendor:publish --tag=max-migrations
php artisan vendor:publish --tag=max-models
php artisan vendor:publish --tag=max-assets
php artisan vendor:publish --tag=max-services

Migrations (after vendor publish)

php artisan migrate

.env

MAX_API_KEY="YOUR_MAX_API_KEY"

Usage

Send message

MAX::sendMessage(
    1234567890, 
    [
        'text' => 'Lorem Ipsum is simply dummy text of the printing and typesetting industry.'
        // there will be more to come...
    ]
);

Send message with MessageBuilder

Max::builder()
    ->chatId(1234567890)
    ->text("Lorem Ipsum is simply dummy text of the printing and typesetting industry.")
    ->attachments(function (AttachmentsBuilder $builder) {
        $builder->image(
            token: "TOKEN_IMAGE"
        );
        $builder->image(store: public_path('image.png'));
        $builder->video(
            token: "TOKEN_VIDEO"
        );
        $builder->inlineKeyboard(
            $builder->row(
                $builder->button->link(
                    text: "Lorem Ipsum 1",
                    url: "https://max.ru",
                ),
                $builder->button->callback(
                    text: "Lorem Ipsum 2",
                    payload: "lorem-ipsum-2",
                )
            ),
            $builder->button->callback(
                text: "Lorem Ipsum 3",
                payload: "lorem-ipsum-3",
            )
        );
    })
    ->send();

Send message with Queue

Max::builder()
    ->chatId(1234567890)
    ->text("Lorem Ipsum is simply dummy text of the printing and typesetting industry.")
    ->attachments(function (AttachmentsBuilder $builder) {
        $builder->image(
            token: "TOKEN_IMAGE"
        );
        $builder->inlineKeyboard(
            $builder->button->callback(
                text: "Lorem Ipsum 1",
                payload: "lorem-ipsum-1",
            )
        );
    })
    ->queue();
$queueMessage = MaxMessageQueue::query()->first();
$queueMessage?->sendQueuedMessage();

or in routes/console.php

use Illuminate\Support\Facades\Schedule;

Schedule::command('max:message-queue:handle')
    ->everyThirtySeconds()
    ->withoutOverlapping();

After this, the Laravel scheduler should be started:

php artisan schedule:work

For production, it is usually run via cron every minute:

* * * * * cd /path/to/project && php artisan schedule:run >> /dev/null 2>&1

Change log

Please see the changelog for more information on what has changed recently.

Testing

composer test

Contributing

Please see contributing.md for details and a todolist.

Security

If you discover any security related issues, please email demettriss@gmail.com instead of using the issue tracker.

Credits

License

license file