skip to Main Content

Sorry to post the code as a screenshot image. Getting error while paste the code here, so I upload the code here
I found this code to send telegram message, but I want to make it to be a function in code igniter’s controller. Can you guys help me?

Sorry for bad English.

Image1: sendMessage.php
Image2: index.php

Thank you.

2

Answers


  1. You can create a new action with name ‘actionSendMessage’, then get POST or GET data.
    Yor function(pic 1) you can move to different

    protected function sendMessage(...$params)
    {
    // here code...
    }

    in your controller

    Login or Signup to reply.
  2. You sould simply make a send_message method on your controller like this :

    /* -----------------------------------------------------
    Simple PHP script for Sending Telegram Bot Message
    ~ Iky | https://www.wadagizig.com
    ------------------------------------------------------ */
    public function send_message()
    {
        /*----------------------
        only basic POST method :
        -----------------------*/
        $telegram_id = $this->input->post('telegram_id');
        $message_text = $this->input->post('message_text');
        /*--------------------------------
        Isi TOKEN dibawah ini: 
        --------------------------------*/
        $secret_token = "622322475:AAGE7HouAbjlr-K4AUrCBCv2bZCNZ0P7Ka4";
    
        $url = "https://api.telegram.org/bot" . $secret_token . "/sendMessage?parse_mode=markdown&chat_id=" . $telegram_id;
        $url = $url . "&text=" . urlencode($message_text);
        $ch = curl_init();
        $optArray = array(
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true
        );
        curl_setopt_array($ch, $optArray);
        $result = curl_exec($ch);
        curl_close($ch);
    
        echo "<script>alert('Pesan berhasil terkirim!'); window.location.href = './';</script>";
    }
    

    And change your_controller_name to whatever controller name that you’ve inserted the above send_message method :

    <!DOCTYPE html>
    <html>
    
    <head>
        <meta charset='utf-8'>
        <title>wadagizig Telegram bots</title>
        <!--link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"-->
    </head>
    
    <body>
    
        <div class="content">
            <section class="content-header">
                <i class="fa fa-home"></i> Home / <i class="fa fa-dashboard"></i> Dashboard / <b>wadagizig</b>
            </section>
            <div class="col-md-4">
                <div class="box box-solid box-primary">
                    <div class="box-header">
                        <h4 class="box-title"><b>Telegram Message <i class="fa fa-send"></i></b></h4>
                    </div>
                    <div class="box-body">
                        <form method="post" action="<?php echo site_url() . 'your_controller_name/send_message' ?>}}">
                            <div class="form-group row">
                                <label class="col-md-4 col-form-label">Telegram ID</label>
                                <div class="col-md-8">
                                    <input type="text" class="form-control" name="telegram_id" placeholder="Telegram ID">
                                </div>
                            </div>
                            <div class="form-group row">
                                <label class="col-md-4 col-form-label">Messages</label>
                                <div class="col-md-8">
                                    <input type="text" class="form-control" name="message_text" placeholder="Custom Text Message">
                                </div>
                            </div>
                            <button type="submit" class="btn btn-primary pull-right">Send <i class="fa fa-send"></i></button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </body>
    
    </html>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search