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

Telegram is a popular cloud-based messaging Russian app. This app is available for Android, iOS, and Windows. Using this app users can send messages, audio, videos, and files. Telegram app stored data on the cloud server and Bot is the outstanding feature of it. More important Telegram app has a feature for optional end-to-end encrypted secret chats.

How a Telegram Bot Works

Telegram Bot is an automated software application that does some tasks repeatedly. This application runs inside the telegram, Using bot API you can manage HTTP requests. You can integrate telegram with another third-party web service, send messages, accept payment.

Create a Telegram Bot Step by Step

Open your Telegram app and search for botfather, it’s a bot itself. It will let you create a telegram bot. Click on the Start option.

Telegram Bot

You can see multiple options on-screen like /newbot, /mybots, /token, /revoke etc.

Click or Type /newbot command on it and it asks for choose a bot name. Enter any relevant bot name, don’t use space in bot name.

Now you will receive a message that the bot is created successfully and the HTTP API token shows. This token is used to authorize the telegram bot API calls. Keep note it down for future use.

Telegram Bot

Test Telegram API Call

To check the API response, let execute a sample API call using the ‘getMe’ method. Just enter below Web URL in the browser. Must enter your bot token in that URL. This will show your bot account a detailed response.

https://api.telegram.org:443/bot{token}/getMe
{
   "ok":true,
   "result":{
      "id":135XXXX042,
      "is_bot":true,
      "first_name":"junzy",
      "username":"junzygram_bot",
      "can_join_groups":true,
      "can_read_all_group_messages":false,
      "supports_inline_queries":false
   }
}

Get Messages sent to your Telegram Bot

There are two API methods to get an update from your bot.

  • getUpdates
  • setWebhook

getUpdates Telegram API Method

This method is used to receive incoming messages from users. It receives messages in JSON format.

<?php

class Chatbot extends CI_Controller {
	
	public function __construct() {
		parent::__construct();
	}

	public function get_update() {
				    
		$token  = '135XXXXXX2:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXbUA';
		$link   = 'https://api.telegram.org:443/bot'.$token.'';
	    
		$update = file_get_contents($link.'/getUpdates');
		$response = json_decode($update, TRUE);
	    
	    $chat_id = $response['result'][0]['message']['chat']['id'];
	    $message = 'Hello There';
	    $parameters = [
	    	'chat_id' => $chat_id, 
	    	'text'  => $message,
	    ];
	    
	    $url = $link.'/sendMessage?'.http_build_query($parameters); 
	    file_get_contents($url);
	    
	}
}
?>

Whenever you execute the above code, it will post a “Hello There” message on your bot. To send a message to the user you have to run this code repeatedly, this is the manual process.

To get this task automate you can use the second-way setWebhook.

setWebhook Telegram API Method

In this method telegram automatically execute the server script. Every time the bot gets a new notification, it sends a webhook response on your set webhook URL. This is the most convenient way to create your Telegram bot.

To automate the chatbot, First set the webhook URL via the setWebhook API method. Use the below web URL to do that, must pass a valid secure web page link in the URL parameter.

https://api.telegram.org:443/bot{token}/setWebhook?
url=https://infovistar.com/api-url

After executing this link in the web browser, you will get a successful response.

{"ok":true,"result":true,"description":"Webhook is already set"}

To receive the telegram bot response you need to get a webhook response in your web server Chatbot.php controller file. Check the below code example.

<?php

class Chatbot extends CI_Controller {
	
	public function __construct() {
		parent::__construct();
	}

	public function callback() {
	    $token  = '135XXXXXX2:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXbUA';
	    $link   = 'https://api.telegram.org:443/bot'.$token.'';
	    
          $getupdate = file_get_contents('php://input'); // for webhook
        
        $update         = json_decode($getupdate, TRUE);
        $chatid         = $update['message']['chat']['id'];
        $req_message    = $update["message"]["text"];   // message from user
	    
	    $chat_id = $response['result'][0]['message']['chat']['id'];
	    
	    $res_message = 'Chatbot message';

	    $parameter = [
	        'chat_id'   => $chatid, 
		    'text'      => $res_message
	    ];

        $request_url = $link.'/sendMessage?'.http_build_query($parameter); 

        file_get_contents($request_url);
	}
}
?>

Whenever anyone writes a message on your bot then this webhook posts “Chatbot message” on your bot as a reply. This is an automated process, doesn’t need any manual action.

Using a Telegram bot API, we have successfully created a chatbot.

To see the complete Bot API description, check this page: https://core.telegram.org/bots/api