Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2022
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. <?php
  2.  
  3. // Токен
  4. const TOKEN = '5751.........:AAEEeLVhp5tj3j5pv...............';
  5.  
  6. // ID чата
  7. const CHATID = "-1001638.....3";
  8.  
  9. // Массив допустимых значений типа файла. Популярные типы файлов можно посмотреть тут: https://docs.w3cub.com/http/basics_of_http/mime_types/complete_list_of_mime_types
  10. $types = array('image/gif', 'image/png', 'image/jpeg', 'application/pdf');
  11.  
  12. // Максимальный размер файла в килобайтах
  13. // 1048576; // 1 МБ
  14. $size = 1048576; // 1 ГБ
  15.  
  16. if ($_SERVER["REQUEST_METHOD"] == "POST") {
  17.  
  18. $fileSendStatus = '';
  19. $textSendStatus = '';
  20. $msgs = [];
  21.  
  22. {
  23.  
  24. // Если не пустые, то валидируем эти поля и сохраняем и добавляем в тело сообщения. Минимально для теста так:
  25. $txt = "";
  26.  
  27. // Не забываем про тему сообщения
  28. if (isset($_POST['theme']) && !empty($_POST['theme'])) {
  29. $txt .= "Тема: " . strip_tags(urlencode($_POST['theme']));
  30. }
  31.  
  32. $textSendStatus = @file_get_contents('https://api.telegram.org/bot'. TOKEN .'/sendMessage?chat_id=' . CHATID . '&parse_mode=html&text=' . $txt);
  33.  
  34. if( isset(json_decode($textSendStatus)->{'ok'}) && json_decode($textSendStatus)->{'ok'} ) {
  35. if (!empty($_FILES['files']['tmp_name'])) {
  36.  
  37. $urlFile = "https://api.telegram.org/bot" . TOKEN . "/sendMediaGroup";
  38.  
  39. // Путь загрузки файлов
  40. $path = $_SERVER['DOCUMENT_ROOT'] . '/telegramform/tmp/';
  41.  
  42. // Загрузка файла и вывод сообщения
  43. $postContent = [
  44. 'chat_id' => CHATID,
  45. ];
  46.  
  47. for ($ct = 0; $ct < count($_FILES['files']['tmp_name']); $ct++) {
  48. if ($_FILES['files']['name'][$ct] && @copy($_FILES['files']['tmp_name'][$ct], $path . $_FILES['files']['name'][$ct])) {
  49. if ($_FILES['files']['size'][$ct] < $size && in_array($_FILES['files']['type'][$ct], $types)) {
  50. $filePath = $path . $_FILES['files']['name'][$ct];
  51. $postContent[$_FILES['files']['name'][$ct]] = new CURLFile(realpath($filePath));
  52. $mediaData[] = ['type' => 'document', 'media' => 'attach://'. $_FILES['files']['name'][$ct]];
  53. }
  54. }
  55. }
  56.  
  57. $postContent['media'] = json_encode($mediaData);
  58.  
  59. $curl = curl_init();
  60. curl_setopt($curl, CURLOPT_HTTPHEADER, ["Content-Type:multipart/form-data"]);
  61. curl_setopt($curl, CURLOPT_URL, $urlFile);
  62. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  63. curl_setopt($curl, CURLOPT_POSTFIELDS, $postContent);
  64. $fileSendStatus = curl_exec($curl);
  65. curl_close($curl);
  66. $files = glob($path.'*');
  67. foreach($files as $file){
  68. if(is_file($file))
  69. unlink($file);
  70. }
  71. }
  72. echo json_encode('SUCCESS');
  73. } else {
  74. echo json_encode('ERROR');
  75. //
  76. // echo json_decode($textSendStatus);
  77. }
  78. } else {
  79. echo json_encode('NOTVALID');
  80. }
  81. } else {
  82. header("Location: /");
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement