<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
yageorgiy / botman-vk-community-callback-driver example snippets
// ...
// Applying driver
DriverManager::loadDriver(\BotMan\Drivers\VK\VkCommunityCallbackDriver::class);
// Applying settings for driver
BotManFactory::create([
"vk" => [
"token" => "REPLACE_ME", // User or community token for sending messages (from Access tokens tab, see above)
"secret" => "REPLACE_ME", // Secret phrase for validating the request sender (from Callback API tab, see above)
"version" => "5.103", // API version to be used for sending an receiving messages (should be 5.103 and higher) (not recommended to change)
"endpoint" => "https://api.vk.com/method/", // VK API endpoint (don't change it if unnecessary)
"confirm" => "", // DEPRECATED SINCE v.1.4.2, LEAVE BLANK (EMPTY STRING) - see 'Mounting & confirming the bot' section. Confirmation phrase for VK
"group_id" => "REPLACE_ME", // Community or group ID
"user_fields" => "" // Extra user fields (see https://vk.com/dev/fields for custom fields) (leave blank for no extra fields) (note: screen_name is already
$botman->on("confirmation", function($payload, $bot){
// Use $payload["group_id"] to get group ID if
$botman->hears('Hello', function ($bot) {
$bot->reply('Hi, '.$bot->getUser()->getFirstName());
});
$botman->hears("What\'s your favourite colour\?", function ($bot) {
$bot->reply('Let me think...');
$bot->typesAndWaits(10);
$bot->reply("I guess it's orange! 😄");
});
use BotMan\BotMan\Messages\Attachments\Image;
use BotMan\BotMan\Messages\Outgoing\OutgoingMessage;
$botman->hears('Gimme some image', function ($bot) {
// Create attachment
$attachment = new Image('https://botman.io/img/logo.png');
// $attachment->addExtras("vk_photo", "photo123456_123456"); // Or send an already uploaded photo (driver will ignore image url)
// Build message object
$message = OutgoingMessage::create('Here it is!')
->withAttachment($attachment);
// Reply message object
$bot->reply($message);
});
use BotMan\BotMan\Messages\Attachments\Video;
use BotMan\BotMan\Messages\Outgoing\OutgoingMessage;
$botman->hears('Gimme some video', function ($bot) {
// Create attachment
$attachment = new Video('http://unused-video-url');
// Attaching already uploaded videos is the ONLY way to send them (as for now):
$attachment->addExtras("vk_video", "video-2000416976_41416976"); // Send an already uploaded video (driver will ignore video url)
// Build message object
$message = OutgoingMessage::create('Here it is!')
->withAttachment($attachment);
// Reply message object
$bot->reply($message);
});
use BotMan\BotMan\Messages\Attachments\Audio;
use BotMan\BotMan\Messages\Outgoing\OutgoingMessage;
$botman->hears('Gimme some audio', function ($bot) {
// Create attachment
// URL can be uploaded ONLY as voice message (due to restrictions of VK)
$attachment = new Audio('https://unused-audio-url');
$attachment->addExtras("vk_audio", "audio371745438_456268888"); // Send an already uploaded audio (driver will ignore audio url and vk_as_voice parameter)
// Build message object
$message = OutgoingMessage::create('Here it is!')
->withAttachment($attachment);
// Reply message object
$bot->reply($message);
});
use BotMan\BotMan\Messages\Attachments\Audio;
use BotMan\BotMan\Messages\Outgoing\OutgoingMessage;
$botman->hears('Sing me a song', function ($bot) {
// Create attachment
// URL can be uploaded ONLY as voice message (due to restrictions of VK)
$attachment = new Audio('https://url-to-ogg-file');
// $attachment->addExtras("vk_audio", "audio371745438_456268888"); // Send an already uploaded audio (driver will ignore audio url and vk_as_voice parameter)
$attachment->addExtras("vk_as_voice", true); // Send as voice message (better to use *.ogg file)
// Build message object
$message = OutgoingMessage::create('Well...')
->withAttachment($attachment);
// Reply message object
$bot->reply($message);
});
use BotMan\BotMan\Messages\Attachments\File;
use BotMan\BotMan\Messages\Outgoing\OutgoingMessage;
$botman->hears("Any files\?", function ($bot) {
$attachment = new File('https://url-to-file');
// $attachment->addExtras("vk_doc", "doc123456_123456"); // Send an already uploaded document (driver will ignore audio url and vk_doc_title, vk_doc_tags parameters)
$attachment->addExtras("vk_doc_title", "Cool guy.gif"); // Title
$attachment->addExtras("vk_doc_tags", "cool, guy"); // Document tags
// Build message object
$message = OutgoingMessage::create('Yep!')
->withAttachment($attachment);
// Reply message object
$bot->reply($message);
});
use BotMan\BotMan\Messages\Attachments\Location;
use BotMan\BotMan\Messages\Outgoing\OutgoingMessage;
$botman->hears('Any locations\?', function($bot) {
// Create attachment
$attachment = new Location(61.766130, -6.822510, [
'custom_payload' => true,
]);
// Build message object
$message = OutgoingMessage::create('Locations are also supported!')
->withAttachment($attachment);
// Reply message object
$bot->reply($message);
});
$botman->hears('Show me the replaced message', function($bot) {
$bot->reply("This string will be ignored", [
"message" => "This message string will be sent"
]);
});
use BotMan\BotMan\Messages\Outgoing\Actions\Button;
use BotMan\BotMan\Messages\Outgoing\Question;
$botman->hears("List of functions\?", function ($bot) {
$question = Question::create('My list of functions:')
->addButtons([
Button::create('Function 1')->value('f1'),
Button::create('Function 2')->value('f2'),
Button::create('Function 3')->value('f3'),
Button::create('Function 4')->value('f4'),
Button::create('Function 5')->value('f5')
]);
$bot->ask($question, function ($answer) {
// Detect if button was clicked:
if ($answer->isInteractiveMessageReply()) {
$selectedValue = $answer->getValue(); // Contains button value e.g. 'f1', 'f2', ...
$selectedText = $answer->getText(); // Contains title e.g. 'Function 1', 'Function 2', ...
}
});
});
//...
$botman->hears("List of functions\?", function ($bot) {
$question = Question::create('My list of functions:')
->addButtons([
Button::create('Function 1')->value('f1')->additionalParameters([
"color" => "secondary" // Colour (see available colours here - https://vk.com/dev/bots_docs_3)
]),
Button::create('Function 2')->value('f2')->additionalParameters([
"color" => "negative"
]),
Button::create('Function 3')->value('f3')->additionalParameters([
"color" => "primary"
]),
Button::create('Function 4')->value('f4')->additionalParameters([
"color" => "primary"
]),
Button::create('Function 5')->value('f5')->additionalParameters([
"color" => "primary"
])
]);
$bot->ask($question, function ($answer) {
//...
});
});
use BotMan\Drivers\VK\Extensions\VKKeyboard;
use BotMan\Drivers\VK\Extensions\VKKeyboardButton;
use BotMan\Drivers\VK\Extensions\VKKeyboardRow;
$botman->hears('keyboard', function(BotMan $bot) {
$keyboard = new VKKeyboard();
$keyboard->setInline(false); // Setting the inline mode ("inline" parameter)
$keyboard->setOneTime(false); // Setting "one_time" parameter
$keyboard->addRows(
// Top row
new VKKeyboardRow([
// Text example
( new VKKeyboardButton() )->setColor("primary")->setText("Sample text")->setValue("button1"),
// UTF-8 text example
( new VKKeyboardButton() )->setColor("primary")->setText("Текст для примера")->setValue("button2"),
]),
// Middle row
new VKKeyboardRow([
// Long text trim example
( new VKKeyboardButton() )
// Colour (see available colours here - https://vk.com/dev/bots_docs_3)
->setColor("default")
// Long text will be trimed with ellipsis at the end of the label
->setText("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam nec ultrices purus, ut sollicitudin arcu.")
// Set button value
->setValue("button3"),
]),
// Bottom row
new VKKeyboardRow([
// Emoji example
( new VKKeyboardButton() )->setColor("negative")->setText("⛔")->setValue("button4"),
( new VKKeyboardButton() )->setColor("primary")->setText("⚠")->setValue("button5"),
( new VKKeyboardButton() )->setColor("positive")->setText("✅")->setValue("button6"),
])
);
$bot->reply("Native keyboard:", [
"keyboard" => $keyboard->toJSON()
]);
});
// ...
$bot->ask($question, function ($answer) {
// Detect if button was clicked:
if ($answer->isInteractiveMessageReply()) {
$selectedValue = $answer->getValue(); // Contains button value e.g. 'f1', 'f2', ...
$selectedText = $answer->getText(); // Contains title e.g. 'Function 1', 'Function 2', ...
}
}, [
"keyboard" => $keyboard->toJSON()
]);
// ...
$botman->receivesImages(function($bot, $images) {
foreach ($images as $image) {
$url = $image->getUrl(); // The direct url
$title = $image->getTitle(); // The title (empty string as titles are not supported by VK)
$payload = $image->getPayload(); // The original payload
$bot->reply("Detected image: {$url}");
}
});
$botman->receivesVideos(function($bot, $videos) {
foreach ($videos as $video) {
$url = $video->getUrl(); // The direct url
$payload = $video->getPayload(); // The original payload
// For YouTube videos title can be accessed in the following way:
$bot->reply("Detected video: {$payload["title"]}");
}
});
$botman->receivesAudio(function($bot, $audios) {
foreach ($audios as $audio) {
$url = $audio->getUrl(); // The direct url
$payload = $audio->getPayload(); // The original payload
$bot->reply("Detected audio: {$url}");
}
});
$botman->receivesFiles(function($bot, $files) {
foreach ($files as $file) {
$url = $file->getUrl(); // The direct url
$payload = $file->getPayload(); // The original payload
$bot->reply("Detected file (document): {$url}");
}
});
$botman->hears('I have both image and video for you.', function ($bot) {
$bot->reply("Cool!");
// Scanning for images
$images = $bot->getMessage()->getImages() ?? [];
foreach ($images as $image) {
$url = $image->getUrl();
$bot->reply("Image found: {$url}");
}
// Scanning for videos
$videos = $bot->getMessage()->getVideos() ?? [];
foreach ($videos as $video) {
$payload = $video->getPayload();
$bot->reply("Video found: {$payload["title"]}");
}
});
$botman->hears('Gimme my photo_200_orig', function ($bot) {
$bot->reply('Here it is: '.$bot->getUser()->getInfo()["photo_200_orig"]);
});
$botman->hears('my info', function(BotMan $bot) {
// Prints raw "client_info" array
$bot->reply(print_r($bot->getMessage()->getExtras("client_info"), true));
});
$botman->hears("Don\'t answer me", function ($bot) {
// Do nothing
});
$botman->on("message_typing_state", function($payload, $bot){
// $payload is an array of the event object ("Object field format"),
// excepting Confirmation event, where $payload contains full root JSON schema.
// See https://vk.com/dev/groups_events for more info
$bot->say("Hey! You're typing something!", $payload["from_id"]);
});
$botman->hears('sticker', function($bot) {
// API method
$endpoint = "messages.send";
// Arguments ("v" and "access_token" are set by driver, no need to define)
$arguments = [
"peer_id" => $bot->getUser()->getId(), // User ID
"sticker_id" => 12, // Sticker ID
"random_id" => 0 //
use BotMan\Drivers\VK\Extensions\VKKeyboardButton;
use BotMan\Drivers\VK\Extensions\VKCarousel;
use BotMan\Drivers\VK\Extensions\VKCarouselActionOpenLink;
use BotMan\Drivers\VK\Extensions\VKCarouselElement;
$botman->hears('carousel', function($bot) {
$carousel = new VKCarousel();
for($i = 1; $i <= 10; $i++){
$carousel->addElements(
new VKCarouselElement(
"Element {$i}",
"Description {$i}",
[
( new VKKeyboardButton() )
->setColor("secondary")->setText("Button {$i}")->setValue("button1")
],
"-00000_11111", // This is an example icon ID:
// replace `00000` with community ID, the `11111` - with image ID
new VKCarouselActionOpenLink("https://some-url/")
)
);
}
$bot->reply("Native carousel:", [
"template" => $carousel->toJSON()
]);
});
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.