Course Content
Introduction to CodeIgniter
CodeIgniter is a powerful PHP framework built for developers who need a simple and elegant toolkit to create full-featured web applications.
0/3
MVC (Model-View-Controller)
MVC stands for Model-View-Controller. MVC is an application design model consisting of three interconnected parts. They include the model (data), the view (user interface), and the controller (processes that handle input).
0/6
Sessions
The Session class allows you to maintain a user’s "state" and track their activity while they browse your site.
0/1
URI Routing
There is a one-to-one relationship between a URL string and its corresponding controller class/method. The segments in a URI normally follow this pattern:
0/1
Forms and Input
Forms provide a way for users to interact with the application and submit data.
0/1
Composer
Composer is dependency manager in PHP. it allows you to declare the libraries your project depends on and it will manage (install/update) them for you.
0/1
Security
You can enable CSRF protection by modifying your application/config/config.php file
0/1
Working with Database
Like any other framework, we need to interact with the database very often and CodeIgniter makes this job easy for us. It provides a rich set of functionalities to interact with the database.
0/5
DataTable
DataTables is a table enhancing plug-in for the jQuery Javascript library that helps in adding sorting, paging, and filtering abilities to plain HTML tables with minimal effort. The main goal is to enhance the accessibility of data in normal HTML tables.
0/1
Spreadsheet
PhpSpreadsheet is a PHP library for reading and writing spreadsheet files. Importing Excel and CSV into MySQL help to save the user time and avoid repetitive work.
0/1
Payment Gateway
Razorpay and PayTM Payment Gateway
0/2
Chatbot
WhatsApp Chatbot and Telegram Chatbot
0/2
CodeIgniter 3
    About Lesson

    Prerequisites

    $ composer require twilio/sdk
    $ composer require guzzlehttp/guzzle

    Next, Go to your Twilio dashboard and copy your ACCOUNT SID and AUTH TOKEN which will be used for authenticating your requests with the Twilio SDK:

    Proceed to update your constant variables with these credentials. Open up your config/constants.php file and add the following variables:

    define('TWILIO_SID', "TWILIO_SID");
    define('TWILIO_AUTH_TOKEN', "TWILIO_AUTH_TOKEN");
    define('TWILIO_WHATSAPP_NUMBER', "+14XXXXXXXX6");

    WhatsApp Chatbot

    Create a Chatbot.php in the applications/controllers/ directory.

    <?php
    defined('BASEPATH') OR exit('No direct script access allowed');
    
    use GuzzleHttpExceptionRequestException;
    use TwilioRestClient;
    // Load Composer's autoloader
    require 'vendor/autoload.php';
    
    class Chatbot extends CI_Controller {
    
        public function __construct() {
            parent::__construct();
        }
    
        public function index() {
        }
    
        public function listenToReplies() {
            $from       = $this->input->post('From');
            $body       = $this->input->post('Body');
            $client     = new GuzzleHttpClient();
            try {
                $response = $client->request('GET', 
                            "https://api.github.com/users/junaidsshaikh");
                $githubResponse = json_decode($response->getBody());
                if ($response->getStatusCode() == 200) {
                    $message = "*Name:* $githubResponse->namen";
                    $message .= "*Bio:* $githubResponse->bion";
                    $message .= "*Lives in:* $githubResponse->locationn";
                    $message .= "*Followers:* $githubResponse->followers devsn";
                    $message .= "*Following:* $githubResponse->following devsn";
                    $message .= "*Number of Repos:* $githubResponse->public_reposn";
                    $message .= "*URL:* $githubResponse->html_urln";
                    $message .= "*Repos URL:* $githubResponse->repos_urln";
                    $this->sendWhatsAppMessage($message, $from);
                } else {
                    $this->sendWhatsAppMessage($githubResponse->message, $from);
                }
            } catch (RequestException $th) {
                $response = json_decode($th->getResponse()->getBody());
                $this->sendWhatsAppMessage($response->message, $from);
            }
        }
    
        public function sendWhatsAppMessage($message, $recipient) {
            $twilio_whatsapp_number = TWILIO_WHATSAPP_NUMBER;
            $account_sid = TWILIO_SID;
            $auth_token = TWILIO_AUTH_TOKEN;
            $client = new Client($account_sid, $auth_token);
            return $client->messages->create($recipient, 
                    array('from' => "whatsapp:$twilio_whatsapp_number", 
                    'body' => $message));
        }
    }

    listenToReplies()

    The listenToReplies() method is where messages sent to your WhatsApp number will be executed. The response sent will rely on the body the received message.

    public function listenToReplies() {
        $from       = $this->input->post('From');
        $body       = $this->input->post('Body');
        $client     = new GuzzleHttpClient();
        try {
            $response = $client->request('GET', 
                        "https://api.github.com/users/junaidsshaikh");
            $githubResponse = json_decode($response->getBody());
            if ($response->getStatusCode() == 200) {
                $message = "*Name:* $githubResponse->namen";
                $message .= "*Bio:* $githubResponse->bion";
                $message .= "*Lives in:* $githubResponse->locationn";
                $message .= "*Followers:* $githubResponse->followers devsn";
                $message .= "*Following:* $githubResponse->following devsn";
                $message .= "*Number of Repos:* $githubResponse->public_reposn";
                $message .= "*URL:* $githubResponse->html_urln";
                $message .= "*Repos URL:* $githubResponse->repos_urln";
                $this->sendWhatsAppMessage($message, $from);
            } else {
                $this->sendWhatsAppMessage($githubResponse->message, $from);
            }
        } catch (RequestException $th) {
            $response = json_decode($th->getResponse()->getBody());
            $this->sendWhatsAppMessage($response->message, $from);
        }
    }

    sendWhatsAppMessage()

    The sendWhatsAppMessage() method uses the Twilio SDK for sending the WhatsApp messages:

    >public function sendWhatsAppMessage($message, $recipient) {
        $twilio_whatsapp_number = TWILIO_WHATSAPP_NUMBER;
        $account_sid = TWILIO_SID;
        $auth_token = TWILIO_AUTH_TOKEN;
        $client = new Client($account_sid, $auth_token);
        return $client->messages->create($recipient, 
                array('from' => "whatsapp:$twilio_whatsapp_number", 
                'body' => $message));
    }

    Setup the Webhook – config/routes.php

    $route['api/chat-bot'] = 'Chatbot/listenToReplies';

    Update the Sandbox Webhook

    Go to the WhatsApp sandbox settings in your Twilio dashboard and update the input field labeled as “WHEN A MESSAGE COMES IN” with the complete URL to your chatbot.

    Your webhook URL must be in the following format:

    https://<hostname>/api/chat-bot

    Output

    WhatsApp chatbot output