Save up to 80 % on our C and C++ e-learning courses. Only this week!
Get up to 50 % extra points for free! More info
c week

Discussion: My "telegram_polling()" and "@message_handler" does not work on "herokuapp.com" under gunicorn

Activities
Avatar
opentek
Member
Avatar
opentek:12/12/2021 8:57

I am writing an application of RobotChat to be 24hours customer-service for answering the questions of the buyers/customers.

I use Python, TelegramAPI, dan Flask.

My program of this-application can work properly if I run it on local-computer (linux-box of SlackWare). And it also works properly if I run my program through HEROKU-CLI:


$ heroku login
$ heroku run bash --app=robotresto
Running bash on ⬢ robotresto... up, run.6958 (Free)
~ $ Python webbasicchatbot.py


Either run my program(webba­sicchatbot.py) through local-computer or run my program($ Python3 webbasicchatbot.py) through Heroku-CLI. The function of "telegram_pollin­g()" and "@message_handler" works properly. My RobotChat(web­basicchatbot.py) can answer each question of the buyers/customers as I expect.

But my problem is:
"
After I deployed my program on 'herokuapp.com', but my program can not give response to any questions. My RobotChat does not answer any question.
"

What is the difference between running "webbasicchat­bot.py" through local-computer and Heroku-CLI to running my program (webbasicchat­bot.py) through gunicorn? It lookslike my "telegram_pollin­g()" and "@message_handler" does not work under gunicorn.

Anybody experienced the same-problem? Would you do me a favor helping me to solve this problem? I am newbie on HEROKU.

You can see it here: https://robotresto.herokuapp.com , my chatbot shows-up but does not respond/answer any single-message of its customers.

<pre>
===
# Procfile
web: gunicorn webbasicchatbot:app1
===
$ nltk.txt
popular
punkt
wordnet
corpora
pros_cons
reuters
hmm_treebank_pos_tagger
maxent_treebank_pos_tagger
universal_tagset
averaged_perceptron_tagger_ru
averaged_perceptron_tagger
snowball_data
rslp
porter_test
vader_lexicon
treebank
dependency_treebank
nltk.downloader
===
# requirements.txt
nltk
Flask
gunicorn
numpy
sklearn
matplotlib
flask_wtf
wtforms_components
pyTelegramBotAPI==3.6.7
PySastrawi
Sastrawi
===
# webbasicchatbot.py
...
...
def telegram_polling():
    try:
        print("Mulai Telegram_Polling")
        mybot.polling(none_stop=True, timeout=6000)
    except:
        traceback_error_string=traceback.format_exc()
        with open("Error-nya_basicchatbot.log", "a") as myfile:
            myfile.write("\r\n\r\n" + time.strftime("%c")+"\r\n<<ERROR polling>>\r\n"+ traceback_error_string + "\r\n<<ERROR polling>>")
        myfile.close()
        mybot.stop_polling()
        time.sleep(10)
        telegram_polling()
...
...
@mybot.message_handler(commands=['help', 'start'])
def send_welcome(pesan):
    mybot.reply_to(pesan, "Selamat-Datang di Restoran-MinangSedap. Untuk daftar-menu selengkapnya, silahkan ketik:\"/daftarmenu\"")
    if(pesan.text=="daftarmenu"): cetakdaftarmenu()

@mybot.message_handler(func=lambda message: True)
def jawab_pesan(pesan:str):
    print("\r\nPertanyaan: " + pesan.text)
    if (pesan.text.lower()=='/daftarmenu'):
        local_daftarmenu=cetakdaftarmenu(pesan.text)
        local_daftarmenutemp=sent_tokenize(local_daftarmenu)
        for baris in local_daftarmenutemp:
            mybot.reply_to(pesan, baris)
        return
    jawaban = bikin_jawabanrandom_pake_array(pesan.text)
    if (jawaban!=''):
        try:
            print("\r\nJawaban dari array-> "+jawaban)
            mybot.reply_to(pesan, jawaban)
            jawaban=''
        except:
            traceback_error_string=traceback.format_exc()
            with open("Error-nya_basicchatbot.log", "a") as myfile:
                myfile.write("\r\n\r\n" + time.strftime("%c")+"GAGAL LEMITASI[lemmatize()]"+ traceback_error_string  + "\r\n")
                myfile.close()
    else:
        local_jawabanrobotchat = bikin_jawaban_pake_pustakadata(pesan.text)
        print("\r\nJawaban RobotChat-> " + local_jawabanrobotchat)
        mybot.reply_to(pesan, local_jawabanrobotchat)
        #mybot.send_message(pesan.chat.id, local_jawabanrobotchat)
        local_jawabanrobotchat=''
...
...
app1 = Flask(__name__, static_url_path = "", static_folder = "static")
env_config = os.getenv("APP_SETTINGS", "config.DevelopmentConfig")
app1.config.from_object(env_config)
SECRET_KEY = os.urandom(32)
app1.config['SECRET_KEY'] = SECRET_KEY
csrf = CSRFProtect(app1)

@app1.route('/', methods=['GET','POST'])
def index():
    secret_key = app1.config.get("SECRET_KEY")
    return render_template("index.html")
...
...
if __name__ == '__main__':
    nltk.download('popular', quiet=True)
    nltk.download("punkt")
    nltk.download("wordnet")
    nltk.download("corpus")
    mytelegram_polling = threading.Thread(name='daemon-myrobotresto', target=telegram_polling)
    mytelegram_polling.setDaemon(True)
    mytelegram_polling.x=1
    mytelegram_polling.start()
    mytelegram_polling.join(6)
    if (mytelegram_polling.is_alive() == True ):
        print("RobotChat is running as a daemon->" + str(mytelegram_polling.is_alive() ) )
        app1.run(host='0.0.0.0', debug=True, threaded=True, use_reloader=False)
    else:
        print("RobotChat dead")

===
</pre>
 
Reply
12/12/2021 8:57
Avatar
opentek
Member
Avatar
 
Up Reply
12/12/2021 8:58
To maintain the quality of discussion, we only allow registered members to comment. Sign in. If you're new, Sign up, it's free.

2 messages from 2 displayed.