Skip to content

Commit

Permalink
python script listens post message
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxNadeev committed Mar 26, 2024
1 parent 9469911 commit 15dfeda
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ sochisto.zip
/node_modules
/.vscode
package-lock.json
package.json
package.json
bot.js
31 changes: 31 additions & 0 deletions mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,35 @@
} else {
echo "Что-то пошло не так при отправке письма 🙁";
}


function sendInTelegramBot($data, $url) {
$options = array(
'http' => array(
'header' => "Content-type: application/json\r\n",
'method' => 'POST',
'content' => json_encode($data)
)
);

$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);

if ($result === FALSE) {
return "Ошибка отправки запроса";
} else {
return $result;
}
}

$data = array(
'name' => $name,
'phone' => $phone
);

$url = '/bot.py';

$response = sendInTelegramBot($data, $url);
echo $response;

?>
43 changes: 43 additions & 0 deletions scripts/bot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# import os
# import telebot
# from telebot import types
from flask import Flask, request, jsonify

# API_KEY = os.getenv('TG_TOKEN')
# u = os.getenv("USER_ID")
# bot = telebot.TeleBot(API_KEY, parse_mode=None)
# response = urllib.request.urlopen(url).read()

# @bot.message_handler(commands=['start'])
# def start(message):
# chatID = message.from_user
# first_name = message.first_name

# if chatID == u:
# mess = f'Добро пожаловать, <b>{first_name}</b>'
# bot.send_message(u.id, mess, parse_mode='html')



app = Flask(__name__)

@app.route('/process_json', methods=['POST'])
def process_json():
data = request.get_json()
name = data.get('name')
phone = data.get('phone')

# Делаем что-то с полученными данными
# Например, выводим их
print(f'Received data - Name: {name}, Phone: {phone}')

return jsonify({'message': 'Data received successfully'})

if __name__ == '__main__':
app.run(debug=True)

# while True:
# try:
# bot.polling()
# except Exception:
# time.sleep(9)

0 comments on commit 15dfeda

Please sign in to comment.