PHP code example of taddy / taddy-sdk

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

    

taddy / taddy-sdk example snippets


use Taddy\Sdk\Taddy;

$taddy = new Taddy(pubId: 'bot-xxxxxxxxxxxxxx', new TaddyOptions(
    token: '123456780:yyyyyyyyyyyyyyyyyy', // Your bot's token
));

use Taddy\Sdk\Dto\User as TaddyUser;

$user = TaddyUser::factory($tgUserId)
    ->withUsername($tgUsername)
    ->withFirstName($tgFirstName)
    // other stuff
;

$taddy->start(
    user: $user, // User DTO
    start: $startMessage // text from update with /start command
);

$ads = $taddy->ads();

// Retrieve ad for $user
$ad = $ads->getAd($user);

// Ad exists, showtime!
if ($ad) {
    $ads->show($ad, $user);
}

// Retrieve ad for $user
$ad = $ads->getAd($user);

// Ad exists, showtime!
if ($ad) {
    // show $ad
    myShowAdFuncation($ad); // your custom method
    
    // send imoressions event
    $ads->impressions($user, $ad->id);
    
    // show ad delay 15 sec.
    sleep(15); 
    
    // hide $ad (delete message)
    myHideAdFuncation($ad); // your custom method
}

$exchange = $taddy->exchange();

$feed = $exchange->getFeed(
    user: $user, // User DTO
    limit: 4, // limit tasks count
    imageFormat: 'png', // png, webp, jpg
    autoImpressions: false, // automatically send impressions event    
);

// send impressions event manually (after successful show)
$exchange->impressionsEvent(
    user: $user, // User DTO
    items: $feed, // showed items array    
);

// send impressions event manually (after successful show)
$exchange->check(
    user: $user, // User DTO
    item: $item, // ExchangeFeedItem DTO or item id   
);

// send custom event
$taddy->customEvent(
    user: $user, // User DTO
    event: 'custom1', // custom event: custom1 ... custom4 
    value: 1.23, // Value (optional)
    currency: Currency::USD, // value currency (optional)
    once: false, // one-time event registration (optional)
);