简体   繁体   English

Python Telegram Bot 仅适用于我,不适用于其他用户

[英]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.我正在使用 Django 构建 Telegram Bot,我已经将它部署到 Heroku,但没有 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.问题是不是每个 Telegram 用户都有用户名,所以当机器人试图在没有用户名的情况下在数据库中创建用户时,它遇到了一个问题,用户名是 null。 As soon as I added logic to handle empty username, everything worked一旦我添加了处理空用户名的逻辑,一切正常

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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