This repository demonstrates the integration of the GeminiAI API into a Viber bot using the Laravel PHP framework. The integration allows the Viber bot to interact with the GeminiAI API to generate responses based on user input.
To set the Viber webhook, use the setWebhook
method in the ViberBotController
. This method makes a POST request to Viber's set_webhook
endpoint, configuring the bot to receive incoming messages.
public function setWebhook(){
#
}
Make sure to replace the placeholders in the code with your Viber authentication token (VIBER_KEY
) and the ngrok endpoint (NGROK_ENDPOINT
).
Incoming Viber webhook events are handled by the webhook
method in the ViberBotController
. This method processes various events, such as user subscriptions, message deliveries, and incoming messages.
public function webhook(Request $request){
#...
}
When a user subscribes to the bot (subscribed
event), a welcome message is sent, and the user is added to the cache to prevent duplicate messages.
public function subscribed($request){
#
}
The sendMessage
method sends text messages to Viber users using the send_message
endpoint.
public function sendMessage($receiver, $text)
The generateMessage
method interacts with the GeminiAI API to generate responses based on user input. It sends a POST request to the Gemini endpoint, passing the user's message and responding with AI generated message.
public function generateMessage($msg)
Replace the placeholders (GEMINI_ENDPOINT
and GEMINI_API_KEY
) with your GeminiAI API endpoint and API key.
Finally configure routes that will handle the request, you only call the set_webhook
route once, and the webhook
will handle most of the incoming request from users who subscribe to the bot.
// Viber
Route::get('viber/set_webhook', [ViberBotController::class,'setWebhook']); // set webhook
Route::post('viber/webhook', [ViberBotController::class,'webhook']);
- Set up your Laravel environment and configure the necessary environment variables.
- Run the
setWebhook
method to activate the Viber webhook. You can run this onpostman
(this is what i recommended). - Handle incoming webhook events and integrate additional logic as needed.
- Customize or Train the GeminiAI integration based on your specific use case.
Feel free to explore and extend the functionality of this Viber bot with GeminiAI integration!