RXTIME commited on
Commit
a527a94
·
verified ·
1 Parent(s): 2fe28a8

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -0
app.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import telegram
3
+ from telegram.ext import Application, CommandHandler
4
+ from flask import Flask
5
+ from threading import Thread
6
+ import schedule
7
+ import time
8
+
9
+ # Inicializa o Flask
10
+ app = Flask(__name__)
11
+
12
+ # Rota básica para manter o Space ativo
13
+ @app.route('/')
14
+ def home():
15
+ return "Hypercycle Bot is running!"
16
+
17
+ # Função para rodar o Flask em uma thread separada
18
+ def run_flask():
19
+ app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 5000)))
20
+
21
+ # Função de exemplo para o comando /start
22
+ async def start(update, context):
23
+ await update.message.reply_text('Hello! I am Hypercycle Bot.')
24
+
25
+ # Função para tarefas agendadas
26
+ def scheduled_task():
27
+ print("Running scheduled task...") # Substitua por sua lógica
28
+
29
+ # Função principal do bot
30
+ def main():
31
+ # Obtém o token do Telegram das variáveis de ambiente
32
+ TELEGRAM_TOKEN = os.environ.get('TELEGRAM_TOKEN')
33
+ if not TELEGRAM_TOKEN:
34
+ raise ValueError("TELEGRAM_TOKEN not set in environment variables")
35
+
36
+ # Configura o bot
37
+ application = Application.builder().token(TELEGRAM_TOKEN).build()
38
+
39
+ # Adiciona handlers de comandos
40
+ application.add_handler(CommandHandler("start", start))
41
+
42
+ # Inicia o bot
43
+ print("Bot is starting...")
44
+ application.run_polling()
45
+
46
+ # Função para rodar o agendador em uma thread separada
47
+ def run_scheduler():
48
+ schedule.every(10).seconds.do(scheduled_task) # Exemplo: tarefa a cada 10s
49
+ while True:
50
+ schedule.run_pending()
51
+ time.sleep(1)
52
+
53
+ if __name__ == '__main__':
54
+ # Inicia o Flask em uma thread
55
+ Thread(target=run_flask).start()
56
+
57
+ # Inicia o agendador em uma thread
58
+ Thread(target=run_scheduler).start()
59
+
60
+ # Inicia o bot
61
+ main()