简体   繁体   中英

Python Telegram Bot works only for me, not for other users

I am building Telegram Bot using Django, I've already deployed it to Heroku, but without Webhooks. For testing I am using polling, and I can't access bot from another telegram account. I have couple of friends who work with me on that project, and I need them to have access to the bot to test it, but when they type anything, bot doesn't reply. I have no idea what the problem is and I haven't found any solution in the internet, so I hope someone will help me here.

def send_search_result(update: Update, context: CallbackContext):
    chat_id = update.message.chat_id
    text = update.message.text

    if not str(text).startswith('/'):

        message = Profile.objects.get_or_create(external_id=chat_id, defaults={'name': update.message.from_user.username})[0]

        Message(profile=message, text=text).save()
    
        update.message.reply_text(text=text)


class Command(BaseCommand):
    help = 'Telegram Bot'

    def handle(self, *args, **options):
        request = Request(con_pool_size=8)

        bot = Bot(request=request, token=settings.TOKEN)

        updater = Updater(bot=bot, use_context=True)

        message_handler = MessageHandler(Filters.text,   send_search_result)
        select_tv_handler = CallbackQueryHandler(callback=select_tv, pass_chat_data=True)

        updater.dispatcher.add_handler(message_handler)
        updater.dispatcher.add_handler(select_tv_handler)

        updater.start_polling()
        updater.idle()   

I've solved the problem. The thing was that not every Telegram user has username, so when bot tried to create a user in database without username it encountered an issue, where username is null. As soon as I added logic to handle empty username, everything worked

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM