Home / API / Telegram Bot API – Search wordpress posts

Telegram Bot API – Search wordpress posts

In this tutorial we will search wordpress posts and list them to the user. If you are new to Telegram Bot check out the Getting started article before reading further. The previous articles on Telegram BOT did not include database connectivity and where just simple enough. Now we will be connecting to the wordpress DB to fetch posts.

This article is the continuation of the previous one. Go through the previous article so that we are on same page

Let’s kick off by configuring the database credentials of our wordpress website in the .env file



Instead of directly writing the code into telegram I am going to check the connectivity of database outside its scope. Follow these steps


Add a route with method ‘get’
Route::get('fetchAllPosts', 'ApiController@fetchallposts');

Laravel is a MVC framework so it is a good practise to write the DB related code and Business model in Model rather writing them in the Controller. So let us now create a Model. In the command prompt navigate to the root folder of laravel project and write the following command


php artisan make:model Post

This will create the model Post.php in the app folder

Now configure the table name in Post model


Controller method

Now that we have configured the table for Post model let’s create a Controller method as set in the Route


Firstly include the Post model in ApiController

use App\Post;

Then write the fetchAll method in Post model


Now navigate to the route http://yourbotdomain.com/fetchAllPosts You should be able to see the list of posts available on your wordpress website.

We are now successfully able to fetch the posts. Our next objective is to include this in the BOT. We will follow a list of steps here

1. Add Search keyboard button to menu
2. Get the user input
3. Search wordpress posts with the user input as search key
4. Structure the data as required and send message to the user


Fetch guidfield from the wp-posts table if you are using default permalink in wordpress. I have used postname in the permalink so I have fetch post_name field. You should structure your date as per your permalink setup



Now modify the webhook method to reflect the following,


In the initiateSearch method create a file with the user’s chat_id as file name XXX2342.txt with search as its only content.

Cancel search: When user hits cancel search button remove the .txt file

Make search: fetch matching posts from database


While making a search we do not need anything else except the post_name field so pluck only post_name.

And that’s it! BOTs are aren’t limited to wordpress bring out your own ideas. Various organizations are working on Artificial Intelligence and Enterprise applications and BOTs have a huge potential in solving problems.


About Ashik

I am a Full Stack Developer and love to work on APIs and Apps. Hardcore lover of Ionic and Laravel <3

Check Also

Private chatroom using socket.io, NodeJS and MySQL

As I promised to write a tutorial on Private chatroom with MySQL DB in the …

Do you wan't to get notified?

I will not spam your inbox. I will only send email whenever I publish a new article or to share information about technology

You have Successfully Subscribed!