Integrate Laravel with ChatGPT and Telegram Bot

Agri Kridanto
6 min readJan 29, 2023

--

Hello Worlds, I want to share about the idea to integrate chatGPT with Telegram Bot, and of course with the Laravel Framework. In the last month, there has been a lot of excitement about ChatGPT, a chat-based AI services developed by OpenAI. The AI system which we can communicate in a conversational way.

Updated on 25/03/2023 : I have add some features on my chatbot as mentioned in my other post.
1. ChatGPT API : Add memory and context of discussion
2. Generating AI Images using Dall-E
3. Transcribe and Translate Audio Data with Whisper API

ChatGPT: Optimizing Language Models for Dialogue

We’ve trained a model called ChatGPT which interacts in a conversational way. The dialogue format makes it possible for ChatGPT to answer followup questions, admit its mistakes, challenge incorrect premises, and reject inappropriate requests. ChatGPT is a sibling model to InstructGPT, which is trained to follow an instruction in a prompt and provide a detailed response. — openai.com

I have tried various things, from: rephrasing a sentence/paragraph, generating outline, summarizing articles, looking for research ideas, and even generating code. It’s so promizing, I think. Honestly, I never imagined AI could be this advanced. The image below explains how the AI model of chatGPT is trained. If you want to try, please visit the openAI page, and click “Try ChatGPT”.

How Chat GPT trained

I do not discuss how chatGPT works or review the service provided by OpenAI. So far, I’ve been using ChatGPT through the PlayGround page. Then an idea emerged, “Can we access the ChatGPT service from our system ?” OpenAI provides an API that we can access through other systems. Let’s create a chatBot that communicates with chatGPT. The system will work like the scenario in the following figure.

System scenario
  1. Generate ChatGPT API KEY

When we first register to access the ChatGPT service, we need to generate an API key. Please create a new key if you don’t have one yet, open the Settings page on the OpenAI website. Please remember this Secret key is confidential, do not share with others !!

OpenAI API Key

To see the usage of Open AI services, you can access the Usage Menu. Attached is my usage for this service in the previous month. Currently, we can still use this service for free and get a credit of 18 USD. It’s worth giving a try. However, this free service has many limitations, such as daily usage limits and sometimes the service is down. It looks like a paid version of chatGPT will be released soon, as reported by TechCrunch.

Open AI Usage Menu

2. Create Telegram Bot

We will use Telegram as our communication channel with ChatGPT so that we can access ChatGPT through the Telegram Platform. So, we will create a Telegram Bot via BotFather.

Create Telegram Bot via BotFather

Ok, please note down your bot token, because we will use the token to interact with our bot via Laravel. If you want to learn Laravel, you can read the tutorial I made on the following page (Indonesian or English).

3. Laravel Package Telegram Bot

To install a package for our Laravel project, please download it using Composer, using the syntax below.

composer require irazasyed/telegram-bot-sdk ^2.0

You can access the documentation at the following link.

By using Telegram Bot and this package, there are important things that need to be considered, namely the method of communication with the Telegram Platform. If we have a system that can be accessed via the internet via HTTPS protocol, then we can use webhooks. Otherwise, we can use the Long-Polling method.

The difference if we use a webhook, Telegram will interact directly with our web via the webhook. So that if a user sends a chat/query, Telegram will immediately forward it to our website and we can carry out the actions and logic needed for the request.

However, if we don’t use webhooks, or so called the Long-Polling method, we must actively get/manage updates stored on the Telegram server. Note to remember, Telegram will only save messages from the last 24 hours.

I prefer webhooks, because they accommodate real time chat. And I use the Domainesia for my hosting, as I explained in the 4th session of my article about Laravel 9.

4. Laravel Package OpenAI

OpenAI provides accessible API services. AFAIK, Open AI provides examples of using the API via Javascript and Python, but there is no example of use for the PHP programming language. Again, with extensive community support, Laravel has many packages that can be used. In this case, to interact with the OpenAI API. You can use the OpenAI package for Laravel and install it via Composer with the following syntax. For more detailed use of the package can be accessed via github.

composer require openai-php/laravel
$client = OpenAI::client('YOUR_API_KEY');

$result = $client->completions()->create([
'model' => 'text-davinci-003',
'prompt' => 'PHP is',
]);

echo $result['choices'][0]['text']; // an open-source, widely-used, server-side scripting language.p

In the above code, we use the API_KEY that we got from OpenAI (Step 1). And we use the model which is ‘text-davinci-3’. Meanwhile, prompts are queries/requests from us, just like our chat via PlayGround ChatGPT. Through this parameter, we will use to interact with users via the Telegram bot. Keep in mind that choosing a model is very important, because it relates to the model we use from ChatGPT, and will greatly affect the performance of the service. And, of course, related to the pricing of these services.

Pricing for ChatGPT Base Models.

5. Result

From the scenario I described above, Telegram bot connects successfully with ChatGPT. Here are some of my random conversations with my new Pal :)

Ok, thats all from me, if you feel this article is helpful please share this post, claps, and follow me on Medium. Please don’t hesitate to ask in the comments section if you have any questions. Have a business idea to implement, similar with this idea? You can DM me via LinkedIn.

Thanks, see you guys on the next posts. Have a good day !!

--

--

Agri Kridanto

I'm a software developer with skills in web & mobile development. Proficient in PHP, Javascript and Java. Also experienced in IoT & Machine Learning research