Files
michaelschiemer/examples/send-telegram-media-example.php
Michael Schiemer 3b623e7afb feat(Deployment): Integrate Ansible deployment via PHP deployment pipeline
- Create AnsibleDeployStage using framework's Process module for secure command execution
- Integrate AnsibleDeployStage into DeploymentPipelineCommands for production deployments
- Add force_deploy flag support in Ansible playbook to override stale locks
- Use PHP deployment module as orchestrator (php console.php deploy:production)
- Fix ErrorAggregationInitializer to use Environment class instead of $_ENV superglobal

Architecture:
- BuildStage → AnsibleDeployStage → HealthCheckStage for production
- Process module provides timeout, error handling, and output capture
- Ansible playbook supports rollback via rollback-git-based.yml
- Zero-downtime deployments with health checks
2025-10-26 14:08:07 +01:00

213 lines
6.7 KiB
PHP
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
declare(strict_types=1);
require_once __DIR__ . '/../vendor/autoload.php';
use App\Framework\Core\AppBootstrapper;
use App\Framework\Notification\Channels\TelegramChannel;
use App\Framework\Notification\ValueObjects\NotificationChannel;
use App\Framework\Notification\Notification;
/**
* Practical Telegram Media Sending Example
*
* This script demonstrates actual media sending via Telegram
* using the MediaManager system
*/
echo "📱 Telegram Rich Media Sending Example\n";
echo "=======================================\n\n";
// Bootstrap application
$app = AppBootstrapper::bootstrap();
$container = $app->getContainer();
// Get Telegram channel with MediaManager
$telegramChannel = $container->get(TelegramChannel::class);
$mediaManager = $telegramChannel->mediaManager;
// Check Telegram capabilities
echo "📋 Telegram Media Capabilities:\n";
$capabilities = $mediaManager->getCapabilities(NotificationChannel::TELEGRAM);
echo " Photos: " . ($capabilities->supportsPhoto ? '✅' : '❌') . "\n";
echo " Videos: " . ($capabilities->supportsVideo ? '✅' : '❌') . "\n";
echo " Audio: " . ($capabilities->supportsAudio ? '✅' : '❌') . "\n";
echo " Documents: " . ($capabilities->supportsDocument ? '✅' : '❌') . "\n";
echo " Location: " . ($capabilities->supportsLocation ? '✅' : '❌') . "\n\n";
// Create notification
$notification = new Notification(
userId: 'user_123',
title: 'Media Test',
body: 'Testing Telegram media capabilities',
channel: NotificationChannel::TELEGRAM,
type: 'media_demo'
);
// Example 1: Send Photo
echo "1⃣ Sending photo...\n";
try {
if ($mediaManager->supportsPhoto(NotificationChannel::TELEGRAM)) {
// Using Telegram's sample photo URL for testing
// In production, use local file paths or previously uploaded file_id
$photoUrl = 'https://api.telegram.org/file/bot<token>/photos/file_0.jpg';
$photoNotification = new Notification(
userId: 'user_123',
title: 'Photo Notification',
body: 'This is a test photo',
channel: NotificationChannel::TELEGRAM,
type: 'photo'
);
$mediaManager->sendPhoto(
NotificationChannel::TELEGRAM,
$photoNotification,
photoPath: $photoUrl, // Can be URL, file path, or file_id
caption: '📸 Test photo from MediaManager'
);
echo " ✅ Photo sent successfully\n";
} else {
echo " ❌ Photo not supported\n";
}
} catch (\Exception $e) {
echo " ⚠️ Error: {$e->getMessage()}\n";
}
echo "\n";
// Example 2: Send Location
echo "2⃣ Sending location...\n";
try {
if ($mediaManager->supportsLocation(NotificationChannel::TELEGRAM)) {
$locationNotification = new Notification(
userId: 'user_123',
title: 'Location Share',
body: 'Meeting point',
channel: NotificationChannel::TELEGRAM,
type: 'location'
);
$mediaManager->sendLocation(
NotificationChannel::TELEGRAM,
$locationNotification,
latitude: 52.5200, // Berlin
longitude: 13.4050,
title: 'Brandenburger Tor',
address: '10117 Berlin, Germany'
);
echo " ✅ Location sent successfully\n";
} else {
echo " ❌ Location not supported\n";
}
} catch (\Exception $e) {
echo " ⚠️ Error: {$e->getMessage()}\n";
}
echo "\n";
// Example 3: Send Document
echo "3⃣ Sending document...\n";
try {
if ($mediaManager->supportsDocument(NotificationChannel::TELEGRAM)) {
$documentNotification = new Notification(
userId: 'user_123',
title: 'Document Share',
body: 'Important document',
channel: NotificationChannel::TELEGRAM,
type: 'document'
);
// In production, use actual file path
// $mediaManager->sendDocument(
// NotificationChannel::TELEGRAM,
// $documentNotification,
// documentPath: '/path/to/document.pdf',
// caption: '📄 Monthly Report',
// filename: 'report_2024.pdf'
// );
echo " Document example (requires actual file path)\n";
} else {
echo " ❌ Document not supported\n";
}
} catch (\Exception $e) {
echo " ⚠️ Error: {$e->getMessage()}\n";
}
echo "\n";
// Example 4: Graceful fallback to text-only
echo "4⃣ Demonstrating graceful fallback...\n";
try {
$fallbackNotification = new Notification(
userId: 'user_123',
title: 'Fallback Test',
body: 'This notification tries to send media, but falls back to text if unsupported',
channel: NotificationChannel::TELEGRAM,
type: 'fallback'
);
// Try to send with photo, fallback to text
if ($mediaManager->supportsPhoto(NotificationChannel::TELEGRAM)) {
echo " Attempting to send with photo...\n";
// $mediaManager->sendPhoto(...);
echo " ✅ Would send photo if file path provided\n";
} else {
echo " Photo not supported, falling back to text notification...\n";
$telegramChannel->send($fallbackNotification);
echo " ✅ Text notification sent as fallback\n";
}
} catch (\Exception $e) {
echo " ⚠️ Error: {$e->getMessage()}\n";
echo " Falling back to text notification...\n";
$telegramChannel->send($fallbackNotification);
echo " ✅ Fallback successful\n";
}
echo "\n";
// Example 5: Using MediaCapabilities for multi-media notifications
echo "5⃣ Smart multi-media notification...\n";
$multiMediaNotification = new Notification(
userId: 'user_123',
title: 'Order Confirmed',
body: 'Your order #12345 has been confirmed',
channel: NotificationChannel::TELEGRAM,
type: 'order_confirmed'
);
$capabilities = $mediaManager->getCapabilities(NotificationChannel::TELEGRAM);
if ($capabilities->supportsPhoto) {
echo " 📸 Could attach product photo\n";
}
if ($capabilities->supportsDocument) {
echo " 📄 Could attach order receipt PDF\n";
}
if ($capabilities->supportsLocation) {
echo " 📍 Could share delivery location\n";
}
echo " ✅ Multi-media notification planned\n\n";
// Summary
echo "✨ Summary\n";
echo "=========\n\n";
echo "MediaManager provides:\n";
echo "- Runtime capability checking before sending\n";
echo "- Type-safe media sending methods\n";
echo "- Graceful fallback support\n";
echo "- Unified API across all channels\n\n";
echo "Best Practices:\n";
echo "1. Always check capabilities before sending media\n";
echo "2. Provide fallback to text notifications\n";
echo "3. Handle exceptions gracefully\n";
echo "4. Use appropriate media types for context\n\n";
echo "✅ Example completed!\n";