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

Connectivity

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=wordpress
DB_USERNAME=mywp-user
DB_PASSWORD=mywp-pass

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

app/Http/routes.php

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

Model

php artisan make:model Post

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

Now configure the table name in Post model

app/Post.php

Controller method

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

app/Http/Controller/ApiController.php

Firstly include the Post model in ApiController

use App\Post;

Then write the fetchAll method in Post model

app/Post.php

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

Important

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

app/Http/Controller/ApiController.php

Webhook

Now modify the webhook method to reflect the following,

Search

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

app/Post.php

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.

Preview

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!