skip to Main Content

I built a Telegram bot with Python and deployed on Heroku.

The app interacts with Telegram via webhook and after that waits for a console input in order to execute additional commands.

The app works properly, but I don’t know how to send make the input command receiving text once the application is deployed on Heroku

This is mainly what the script does:

import os

import telegram
from telegram.ext import CallbackContext
from telegram.ext import MessageHandler, Filters
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update, KeyboardButton, ReplyKeyboardMarkup
from telegram.ext import Updater, CommandHandler, CallbackQueryHandler

PORT = os.environ.get('PORT', 8443)

token = 'MY-TOKEN'
updater = Updater(token=token, use_context=True)
dispatcher = updater.dispatcher

def start_func(update: Update, context: CallbackContext):
   ...do something when /start is received ...
def message_func(update: Update, context: CallbackContext):
   ...do something when message is received ...
            
dispatcher.add_handler(MessageHandler(Filters.text & (~Filters.command), message_func))
dispatcher.add_handler(CommandHandler('start', start_func))

updater.start_webhook(listen="0.0.0.0",
                              port=int(PORT),
                              url_path=test_token,
                                  webhook_url=f'https://{appname}.herokuapp.com/{test_token}')
while (True):
    print("Enter a command: ")
    command = input()
    if command == "test":
        .... do something ...
 

2

Answers


  1. Sadly, you can’t take direct arguments in Heroku.

    But, here are some methods / Hacks you can achieve your goal.

    • Take arguments from telegram itself -> you can easily take arguments from telegram itself. if you are worried about security why not gives access only to specific user by comparing user ids and make a admin like token system.

    • Use web dashboards -> you could easily make a web dashboard to do this. the entity can send requests to the heroku app url and give inputs.

    I strongly recommend you to create a web dashboard with token system which seems very efficient and secure!

    Login or Signup to reply.
  2. you can try pythonanywhere,
    it works properly on there

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search