0
0
0906ddd2021-09-07 10:33:16
PHP
0906ddd, 2021-09-07 10:33:16

How to transfer a file to telegram?

the bot itself was created
_________________

there is a field with an input type file (screen below)

6137207c50d21498204568.jpeg

you need to transfer it to telegrams, as well as the data from the inputs I pass the

inputs (they are displayed successfully)


$('.button').on('click', function(e) {
   
        var phone  = $('#c_phone-number').val();
        var email = $('#c_email').val();  
        
            $.ajax({
                url: myajax.url,
                type: 'POST',
                data: {
                    action: 'request',
                    phone: phone, 
                    email: email,
                },
              
             }).done(function(data){

                $('#c_phone-number, #c_email ).val('');

            });
         
     return false;
        
    });


on the server like this

<?php 
add_action('wp_ajax_request', 'request_action');
add_action('wp_ajax_nopriv_request', 'request_action');

function request_action() {
 	$chat_id = 'свой номер чата';
  $token = 'свой номер токена';

  $phone = $_POST['phone'];
  $email = $_POST['email'];

  $text .= "<b>Сообщение с сайта</b>\n\n";
  $text .= "Телефон: ".$phone."\n";
  $text .= "Email: ".$email."\n";

  $text = urlencode($text);


  $str = "https://api.telegram.org/bot".$token."/sendMessage?chat_id=".$chat_id."&parse_mode=html&text=".$text;

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $str);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($ch);
    curl_close($ch);

    wp_die();
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
nokimaro, 2021-09-07
@nokimaro

Use the sendDocument
method Limitation - file size up to 50 MB
https://core.telegram.org/bots/api#senddocument

A
Artyom Prikhodko, 2021-09-08
@paganez

And use wp_remote_get / wp_remote_post functions to send requests.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question