Update main.py
Browse files
main.py
CHANGED
@@ -1,26 +1,37 @@
|
|
1 |
-
import telebot
|
2 |
import os
|
|
|
|
|
3 |
|
4 |
-
|
5 |
-
TOKEN = os.getenv('TELEGRAM_TOKEN')
|
6 |
|
7 |
-
|
8 |
-
|
9 |
|
10 |
-
# Инициализация бота
|
11 |
-
bot = telebot.TeleBot(
|
12 |
|
13 |
-
#
|
14 |
@bot.message_handler(commands=['start'])
|
15 |
def send_welcome(message):
|
16 |
-
bot.reply_to(message, "Привет! Я твой Telegram
|
17 |
|
18 |
-
#
|
19 |
@bot.message_handler(func=lambda message: True)
|
20 |
def echo_all(message):
|
21 |
bot.reply_to(message, f"Ты сказал: {message.text}")
|
22 |
|
23 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
if __name__ == '__main__':
|
25 |
-
|
26 |
-
bot.polling(none_stop=True)
|
|
|
|
|
1 |
import os
|
2 |
+
from flask import Flask, request
|
3 |
+
import telebot
|
4 |
|
5 |
+
app = Flask(__name__)
|
|
|
6 |
|
7 |
+
# Получаем токен Telegram бота из переменной окружения
|
8 |
+
TELEGRAM_TOKEN = os.getenv('TELEGRAM_TOKEN')
|
9 |
|
10 |
+
# Инициализация бота
|
11 |
+
bot = telebot.TeleBot(TELEGRAM_TOKEN)
|
12 |
|
13 |
+
# Функция обработки команды /start
|
14 |
@bot.message_handler(commands=['start'])
|
15 |
def send_welcome(message):
|
16 |
+
bot.reply_to(message, "Привет! Я твой Telegram бот!")
|
17 |
|
18 |
+
# Функция обработки текстовых сообщений
|
19 |
@bot.message_handler(func=lambda message: True)
|
20 |
def echo_all(message):
|
21 |
bot.reply_to(message, f"Ты сказал: {message.text}")
|
22 |
|
23 |
+
# Роут для приема обновлений от Telegram
|
24 |
+
@app.route(f'/{TELEGRAM_TOKEN}', methods=['POST'])
|
25 |
+
def webhook():
|
26 |
+
json_str = request.get_data(as_text=True)
|
27 |
+
update = telebot.types.Update.de_json(json_str)
|
28 |
+
bot.process_new_updates([update])
|
29 |
+
return "OK", 200
|
30 |
+
|
31 |
+
# Роут для проверки
|
32 |
+
@app.route('/')
|
33 |
+
def index():
|
34 |
+
return 'Telegram bot is running!', 200
|
35 |
+
|
36 |
if __name__ == '__main__':
|
37 |
+
app.run(host='0.0.0.0', port=5000)
|
|