PHP code example of ctapu4ok / vk-messenger-sdk

1. Go to this page and download the library: Download ctapu4ok/vk-messenger-sdk 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/ */

    

ctapu4ok / vk-messenger-sdk example snippets


 declare(strict_types=1);

if (file_exists('vendor/autoload.php')) {
    er;
use ctapu4ok\VkMessengerSdk\Settings;

enum Params
{
    public const API_HASH = 'vk1.a.Qyw6zef4YQZmosPX5J.....';
    public const GROUP_ID = 12345678;
    public const CONFIRM_STRING = 'c683e9eb12cebb65ce.....';

    public const VERSION = '5.81';
}

class MessengerEvent extends EventHandler
{
    public function onStart(): void
    {
        $this->getAPI()->logger('The event handler was initialized');
    }
    public function messageEvent(int $group_id, ?string $secret, array $object): void
    {
        $this->getAPI()->logger([
            'New message event received', $object
        ], Logger::LOGGER_CALLABLE);
    }
    public function messageNew(int $group_id, ?string $secret, array $object): void
    {
        $this->getAPI()->logger([
            'New message received: ', $object
        ], Logger::LOGGER_CALLABLE);

        /**
         * @var $this->getVk() The main VK API methods src/API/Actions
         */
        $msg_id = $this->getVk()->messages()->send([
            'user_id' => $object['message']['from_id'],
            'random_id' => floor(microtime(true) * 1000),
            'peer_id' => $object['message']['peer_id'],
            'message' => 'Hello World!'
        ]);

        $this->getAPI()->logger([
            'Getting Message ID: ', $msg_id
        ], Logger::LOGGER_CALLABLE);
    }

    public function messageTypingState(int $group_id, ?string $secret, array $object): void
    {
        $this->getAPI()->logger([
            'The user started typing a message', $object
        ], Logger::LOGGER_CALLABLE);
    }
    
    /**
    * Cron example  
    */ 
    #[Cron(period: 5)]
    public function testingCron5()
    {
        $this->getAPI()->logger([
            'THIS IS CRON 5!!!!!!'
        ], Logger::LOGGER_CALLABLE);
    }

    #[Cron(period: 1)]
    public function testingSome1()
    {
        $this->getAPI()->logger([
            'THIS IS CRON 1.0!!!!!!'
        ], Logger::LOGGER_CALLABLE);
    }
}

$Settings = new Settings();

$Settings->getAppInfo()->setApiHash(Params::API_HASH);
$Settings->getAppInfo()->setGroupId(Params::GROUP_ID);
$Settings->getAppInfo()->setConfirmString(Params::CONFIRM_STRING);
$Settings->getAppInfo()->setApiVersion(Params::VERSION);

// we say to output logs to a file (without console)
//$Settings->getLogger()
//    ->setType(Logger::LOGGER_FILE)
//    ->setExtra('log.file')
//    ->setMaxSize(50*1024*1024);
// Database settings
//$Settings->setDb(
//    (new Settings\Database\Mysql())
//    ->setUri('127.0.0.1:3306')
//    ->setDatabase('vk_messenger')
//    ->setUsername('root')
//    ->setPassword('root')
//);

MessengerEvent::loop($Settings);