Last active
January 28, 2020 07:28
-
-
Save Aldegid/ec215c92fedf1a1046e1ea4f1889293c to your computer and use it in GitHub Desktop.
send form to telegram
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function sendMessage() { | |
| var formDataFile = new FormData(); | |
| var result = {} | |
| result['location'] = `Новая заявка с сайта: ${window.location.origin}${window.location.pathname}` | |
| if (document.querySelector('form').hasAttribute('data-form-title')) { | |
| result['vacancy'] = `Вакансия: ${$('form').attr('data-form-title')}` | |
| } | |
| $('form input').each(function() { | |
| var inputType = $(this).attr('type'); | |
| if ((inputType !== 'radio' && inputType !== 'file' && $(this) | |
| .val().length !== 0) || (inputType === 'radio' && $(this).is(':checked'))) { | |
| var inputName = $(this).attr('name'), | |
| inputlabel = $(this).attr('data-name'), | |
| inputValue = $(this).val(); | |
| result[inputName] = `${inputlabel}: ${inputValue}` | |
| } | |
| if (inputType === 'file' && $(this).val().length !== 0) { | |
| var inputName = $(this).attr('name'), | |
| file = $(this)[0].files[0], | |
| fileName = $(this)[0].files[0].name; | |
| formDataFile.append(inputName, file, fileName); | |
| } | |
| }); | |
| formDataFile.append('data', JSON.stringify(result)); | |
| ajaxRequest({ | |
| data: formDataFile, | |
| contentType: false, | |
| processData: false | |
| }) | |
| .then(function(res) { | |
| console.log('Message sent to telegram'); | |
| }) | |
| .catch(function(err) { | |
| console.log(err); | |
| }); | |
| } | |
| function ajaxRequest(config) { | |
| return new Promise(function(resolve, reject) { | |
| $.ajax({ | |
| type: 'POST', | |
| url: '../send.php', | |
| data: config.data, | |
| cache: false, | |
| enctype: 'multipart/form-data', | |
| contentType: config.contentType, | |
| processData: config.processData, | |
| success: function(res) { | |
| resolve(res); | |
| }, | |
| error: function(err) { | |
| reject(err); | |
| } | |
| }); | |
| }); | |
| } | |
| /*PHP*/ | |
| <?php | |
| define('TELEGRAM_TOKEN', 'YOUR_TOKEN'); // Test | |
| define('TELEGRAM_CHATID', 'YOUR_CHAT_ID'); | |
| function SendTelFile($type, $data) { | |
| if ($type == 'sendDocument') { | |
| $RealTitleID = $_FILES[$data]['name']; | |
| $data = new CurlFile($_FILES[$data]['tmp_name'], 'file/exgpd', $RealTitleID); | |
| } | |
| $ch = curl_init(); | |
| curl_setopt_array($ch, [ | |
| CURLOPT_URL => 'https://api.telegram.org/bot'.TELEGRAM_TOKEN.'/'.$type, | |
| CURLOPT_RETURNTRANSFER => true, | |
| CURLOPT_SSL_VERIFYPEER => false, | |
| CURLOPT_HTTPHEADER => [ | |
| 'Content-Type: multipart/form-data' | |
| ], | |
| CURLOPT_POST => true, | |
| CURLOPT_POSTFIELDS => [ | |
| 'chat_id' => TELEGRAM_CHATID, | |
| 'text' => $data, | |
| 'parse_mode' => 'html', | |
| 'document' => $data | |
| ] | |
| ]); | |
| $out = curl_exec($ch); | |
| curl_close($ch); | |
| print_r($out); | |
| } | |
| foreach($_POST as $field) { | |
| foreach(json_decode($field) as $gg) { | |
| $message .= "<b>".$gg."</b>\n"; | |
| } | |
| } | |
| SendTelFile('sendMessage', $message); | |
| SendTelFile('sendDocument', 'cv'); | |
| SendTelFile('sendDocument', 'portfolio'); | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment