【问题标题】:How to have the message text showed up in my command line?如何在我的命令行中显示消息文本?
【发布时间】:2021-01-06 01:53:25
【问题描述】:

我使用 pytelegrambotapi 库在电报中制作我的机器人。

这是我的代码:

import config
import telebot

bot = telebot.TeleBot(config.token)


@bot.message_handler(commands=['start'])
def handle_start_message(message):
    bot.send_message(message.chat.id, "Hello. I'm your bot")
    print(message.text)

我想要做的是在我的命令行中返回消息文本。它什么也不返回,但是,机器人正在工作,因为它会在电报中回复我。

【问题讨论】:

  • 你试过pythonlogging模块吗?

标签: python telegram py-telegram-bot-api


【解决方案1】:

这个可行,但它只会吐出整个内容。

@bot.message_handler(func=lambda message: True)
def echo_message(message):
    print(message)
    bot.reply_to(message, message.text)

我正在努力寻找更好的替代方案,希望我能让这个过程变得简单。

##updated
print(message.text)

我已经测试过了。

【讨论】:

    【解决方案2】:

    很可能,代码在单独的 Tread/Process 中运行,它无法访问控制台,因此无法在其中打印某些内容。

    使用日志记录而不是打印。

    这是一个有用的链接 - https://github.com/python-telegram-bot/python-telegram-bot#logging

    【讨论】:

    • thx,但实际上我只使用 print 来表明机器人接收到的消息会影响某些局部变量的事实。比如:如果 message.text == "hello": local_var =1
    猜你喜欢
    • 2012-04-06
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 2014-07-06
    • 2013-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多