PHP code example of alexkargin / msteams-bot-php

1. Go to this page and download the library: Download alexkargin/msteams-bot-php 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/ */

    

alexkargin / msteams-bot-php example snippets




use TeamsBot\Bot;
use TeamsBot\Exception\TeamsBotException;
use TeamsBot\Exception\TeamsBotTokenException;

try {
    $bot = new TeamsBot\BotListener('bot_id', 'password');

    // Handled on any request
    $bot->onAny(static function (Bot $bot) {
        // Sends a simple text message
        if(!empty($bot->context->getText())) {
            $bot->reply('You send ' . $bot->context->getText());
        }                     
    });

    // Handled when user add bot
    $bot->onStartPersonalChat(static function (Bot $bot) {
        // Sends a simple text message
        $bot->reply('Hi, ' . $bot->context->getFromName());                    
    });

    // Handled when user sends 'test' to bot
    $bot->onText('test', function (Bot $bot) {
        // create Activity
        $message = $bot->createMessage();
        // add Hero Card
        $att = new TeamsBot\Card\HeroCard();
        $att->setContentFromJson('
{
"buttons": [
    {
        "type": "messageBack",
        "text": "Send request to bot",
        "value": "{\"property\": \"propertyValue\" }"
   }
]
}
        ');
        $message->addAttachment($att);
        // send new message
        $bot->postMessage($message);
    });

    // Handled when user send form to bot
    // for example, Hero Card from previous handler
    $bot->onSubmitForm(function (Bot $bot) {
        $message = $bot->createMessage();
        $message->setText('Received data: ' . json_encode($bot->context->getFormData(), JSON_THROW_ON_ERROR));
        // update Activity with form
        $bot->updateMessage($message);
    });

} catch (TeamsBotException $e) {
} catch (TeamsBotTokenException $e) {

}

    $bot = new TeamsBot\BotListener('bot_id', 'password');
    // use filesystem driver
    $pool = new Stash\Pool(new Stash\Driver\FileSystem([]));
    $item = $pool->getItem('token');
    $token = $item->get();
    if($item->isMiss())
    {
        // get new token
        $token = $bot->token->get();
        // Cache expires $token['expires_in']
        $expiration = new DateTime('@'.$token['expires_in']);
        $item->expiresAfter($expiration);
        $item->set($token);
        $pool->save($item);
    }

    // set token
    $bot->token->set($token);
bash
composer