CrazeBots.

How to send a PDF file to a Telegram user in PHP

By admin | Published on October 1, 2024 | tutorials
Blog Post Image

WhatsMate API Technical Guide

Logo

How to send a PDF file to a Telegram user in PHP

Using the WhatsMate Telegram Gateway REST API

Posted on June 20, 2022   Share on Facebook Share on LinkedIn Share on Twitter Share on Google+

Last reviewed: April 17, 2024  

This article shows you how to send a document (e.g. a PDF file) to a registered Telegram user in PHP.

Before the recipient can receive your Telegram message, she MUST register with the WhatsMate Telegram Gateway. Instructions are available on the official siteUnregistered users will never receive messages from the Gateway.

To send a Telegram message containing a PDF document in PHP, do this:

  1. First, learn how to send a simple text message on the official site.
  2. With the above knowledge, you can now download our sample code.
  3. Locate the file php/send-telegram-pdf.php.
      <?php
      $INSTANCE_ID = 'YOUR_INSTANCE_ID_HERE'; // TODO: Replace it with your gateway instance ID here
      $CLIENT_ID = "YOUR_CLIENT_ID_HERE"; // TODO: Replace it with your Premium client ID here
      $CLIENT_SECRET = "YOUR_CLIENT_SECRET_HERE"; // TODO: Replace it with your Premium client secret here
       
      $pathToDocument = "/tmp/your_doc.pdf"; // TODO: Replace it with the path to your document
      $docData = file_get_contents($pathToDocument);
      $base64Doc = base64_encode($docData);
      $fn = "anyname.pdf"; // TODO: Replace it with any name you like
      $caption = "Check this out"; // TODO: Replace it with an eye-catching caption
       
      $postData = array(
      'number' => '12025550108', // TODO: Specify the recipient's number (NOT the gateway number) here.
      'document' => $base64Doc,
      'filename' => $fn,
      'caption' => $caption
      );
       
      $headers = array(
      'Content-Type: application/json',
      'X-WM-CLIENT-ID: '.$CLIENT_ID,
      'X-WM-CLIENT-SECRET: '.$CLIENT_SECRET
      );
       
      $url = 'https://api.whatsmate.net/v3/telegram/single/document/message/' . $INSTANCE_ID;
      $ch = curl_init($url);
      curl_setopt($ch, CURLOPT_POST, 1);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
      curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData));
      $response = curl_exec($ch);
      echo "Response: ".$response;
      curl_close($ch);
      ?>

    view rawsend-telegram-pdf.php hosted with ❤ by GitHub

  4. Study the PHP source code and customize the TODO/FIXME lines.
    • Among other things, put down YOUR OWN number as the recipient. You can’t send messages to strangers because we are against SPAMMERS.
  5. Visit the PHP page your just created to send your PDF file.

Happy coding :)

 

Tags:
← Back to Blog