Persistent problems with depolyment

Question:

If have a problem that so far my programs, which run just fine, never manage to deploy. Could you please tell me if I do something wrong on principle, or where my mistakes are that let the deployment break down? It always fails in the promotion stage.
Generally I have to say that the removal of the “always on” function is providing big problems for me.

I tried to deploy as Autoscale, my run command is: python3 main.py

It dies with the following error message:

01/03/24 11:13:16 AM
hostingpid1: an open port was not detected

Please let me know which information you need from me to solve this problem for me.

Repl link/Link to where the bug appears:

TX-Mistral Repl

Screenshots, links, or other helpful context:

Error Log

01/03/24 11:13:16 AM
hostingpid1: an open port was not detected
01/03/24 11:13:06 AM
2024-01-03 10:13:06,678 - httpx - INFO - HTTP Request: POST ...
"HTTP/1.1 200 OK"
01/03/24 11:12:56 AM
2024-01-03 10:12:56,558 - httpx - INFO - HTTP Request: POST ...
"HTTP/1.1 200 OK"
01/03/24 11:12:46 AM
2024-01-03 10:12:46,442 - httpx - INFO - HTTP Request: POST ...
"HTTP/1.1 200 OK"
01/03/24 11:12:36 AM
2024-01-03 10:12:36,470 - httpx - INFO - HTTP Request: POST ...
"HTTP/1.1 200 OK"
01/03/24 11:12:22 AM
2024-01-03 10:12:22,566 - httpx - INFO - HTTP Request: POST ...
 "HTTP/1.1 200 OK"
01/03/24 11:12:22 AM
2024-01-03 10:12:22,217 - telegram.ext.Application - INFO - Application started
01/03/24 11:12:22 AM
2024-01-03 10:12:22,215 - httpx - INFO - HTTP Request: POST ...
"HTTP/1.1 200 OK"
01/03/24 11:12:22 AM
2024-01-03 10:12:22,103 - httpx - INFO - HTTP Request: POST ...
 "HTTP/1.1 200 OK"
01/03/24 11:12:15 AM
starting up user application

Hi @MartinSchrocker , welcome to the forums!
If you are deploying a bot, you’ll need to deploy it as a Reserved VM, not Autoscale or Static.
Hope this helps!

1 Like

Replicated in deployments listens on the “0.0.0.0” port. You need to understand your port to “0.0.0.0” in the code in order for the site to start working.

1 Like

Thanks a lot for the reply! This seems to be the problem, since I get the same error message also with the Reserved VM unfortunately.

However, I do not know what to do now. Can you tell me wherre to set the port, and what I need to set it to?

Below the code I use for the telegram bot, which I suppose is where the problem comes from:

# Telegram bot
import logging
from telegram import Update
from telegram.ext import filters, MessageHandler, ApplicationBuilder, CommandHandler, ContextTypes

logging.basicConfig(
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    level=logging.INFO
)

async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
    await context.bot.send_message(chat_id=update.effective_chat.id, text="I'm a bot, please talk to me!")

async def echo(update: Update, context: ContextTypes.DEFAULT_TYPE):

    response_text = generate_chat_completion([
        {"role": "system", "content": "You are my digital familiar called Turminder Xuss. You are by my side and help me in everything."},
        {"role": "user", "content": update.message.text}
    ])

    await context.bot.send_message(chat_id=update.effective_chat.id, text=response_text)

if __name__ == '__main__':
    application = ApplicationBuilder().token(telegramkey).build()
    
    start_handler = CommandHandler('start', start)
    echo_handler = MessageHandler(filters.TEXT & (~filters.COMMAND), echo)
    
    application.add_handler(start_handler)
    application.add_handler(echo_handler)

    application.run_polling()

It works now, I added the following code:

# Flask app to avoid Replit sleeping
app = Flask('')

@app.route('/')
def home():
    return "I'm alive!"

def run():
    app.run(host='0.0.0.0', port=8080) # The port can be changed here

def keep_alive():
    t = Thread(target=run)
    t.start()

And in the if __name__ == ‘__main__’: section

keep_alive()  # Call function to run the Flask app

Thanks for the help everyone!

Glad it works! Another alternative would be to select the “Background Worker” option instead of the “Web Server” option when deploying your Reserved VM.

2 Likes

Indeed it fixed my problem as well, thanks. So the solution is not to buy a VM, it’s to have a flask around

Wait but where does your bot code go? In which route? Or is it a separate file?