You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Laravel框架的Bot能否实现从Telegram Bot向Viber Bot发消息?

Is it possible to forward messages from a Telegram Bot to a Viber Bot with Laravel?

Absolutely feasible! This is a straightforward integration since both platforms expose REST APIs that play nicely with Laravel's built-in HTTP client. Here's a step-by-step guide to implement this:

1. Prep your API credentials

First, gather the necessary tokens from both platforms:

  • Telegram: Your bot's token (get this from @BotFather)
  • Viber: Your bot's authentication token, plus the target Viber user ID (you can get this when a user interacts with your Viber bot via webhook events)

Store these in your Laravel .env file for security:

TELEGRAM_BOT_TOKEN=your_telegram_bot_token_here
VIBER_AUTH_TOKEN=your_viber_auth_token_here
VIBER_TARGET_USER_ID=user_id_of_the_viber_recipient
VIBER_BOT_NAME=YourBotName

2. Set up Telegram webhook to receive messages

Telegram uses webhooks (or polling) to send incoming messages to your bot. For Laravel, create a controller to handle the webhook payload:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;

class CrossBotController extends Controller
{
    public function handleTelegramWebhook(Request $request)
    {
        // Extract the incoming message from Telegram
        $incomingMessage = $request->input('message.text');
        $telegramChatId = $request->input('message.chat.id');

        // Skip if there's no text content
        if (empty($incomingMessage)) {
            return response('No message content to forward', 400);
        }

        try {
            // Forward the message to Viber using their Send Message API
            $viberResponse = Http::withHeaders([
                'X-Viber-Auth-Token' => env('VIBER_AUTH_TOKEN'),
                'Content-Type' => 'application/json',
            ])->post('https://chatapi.viber.com/pa/send_message', [
                'receiver' => env('VIBER_TARGET_USER_ID'),
                'type' => 'text',
                'text' => $incomingMessage,
                'sender' => [
                    'name' => env('VIBER_BOT_NAME'),
                    // Optional: Add an avatar URL for your Viber bot
                    // 'avatar' => 'https://your-bot-avatar-url.com/image.jpg'
                ]
            ]);

            // Log and handle Viber API errors
            if (!$viberResponse->successful()) {
                Log::error('Viber API Error: ' . $viberResponse->body());
                return response('Failed to forward message to Viber', 500);
            }

            // Optional: Send a confirmation back to the Telegram user
            Http::post(
                "https://api.telegram.org/bot" . env('TELEGRAM_BOT_TOKEN') . "/sendMessage",
                [
                    'chat_id' => $telegramChatId,
                    'text' => '✅ Message successfully forwarded to Viber!'
                ]
            );

            return response('OK', 200);
        } catch (\Exception $e) {
            Log::error('Forwarding failed: ' . $e->getMessage());
            return response('Internal server error', 500);
        }
    }
}

3. Configure routes

Add a route in routes/api.php to point to your controller:

Route::post('/telegram/webhook', [App\Http\Controllers\CrossBotController::class, 'handleTelegramWebhook']);

4. Key considerations

  • HTTPS Requirement: Telegram requires webhook URLs to be HTTPS. If you're developing locally, use a tool like ngrok to expose your Laravel server over HTTPS.
  • Viber User Interaction: Viber only allows bots to send messages to users who have previously initiated a conversation with the bot. Make sure your target user has chatted with your Viber bot first.
  • Message Types: The example above handles text messages, but you can extend it to support images, stickers, etc. Just map Telegram's message formats to Viber's API requirements.
  • Error Handling: Always log API failures and consider adding retries for transient errors.

内容的提问来源于stack exchange,提问作者Pavel Pavel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 15:13:11