【问题标题】:Telethon doesn't send messages when running as serviceTelethon 在作为服务运行时不发送消息
【发布时间】:2021-11-20 22:16:31
【问题描述】:

我创建了一个使用 Telethon 发送消息的脚本。接收者并不总是相同的:接收者的数量和它们的 ID 取自 MySQL 表。从命令提示符启动时,多处理脚本在预期循环中运行良好。但是一旦它作为服务启动,消息就不会发送。

请参阅下面的代码,其中包含发送消息的功能。该函数被另一个函数调用,该函数循环一个 MySQL 查询的结果。

有人能解释一下为什么函数在提示符下运行良好而不是作为服务运行吗?

import configparser

# get configuration
config = configparser.ConfigParser()
config.read('/etc/p2000.cfg')
telegram_api_id = config.get('telegram','api_id')
telegram_api_hash = config.get('telegram','api_hash')
telegram_bot_name = config.get('telegram','bot_name')

client = TelegramClient(telegram_bot_name, telegram_api_id, telegram_api_hash)

def p2k_send_telegram(PeerID,Message):
    async def main():
        await client.send_message(int(PeerID), Message)


    with client:
        client.loop.run_until_complete(main())

【问题讨论】:

  • 我认为“作为服务”是指 systemd、initd 或类似的东西?
  • 是的,正确。它与 systemd 一起运行。
  • 要解决问题,我们需要找到问题的根源。首先启用logging 并使用日志更新问题。

标签: python-3.x telethon


【解决方案1】:

好的,答案很简单,就在我面前!该问题可能与客户端变量有关。在 systemd 下作为服务运行时,必须使用完整路径定义会话(文件)!

类似这样的:

client = TelegramClient('/full/path/to/my.session', telegram_api_id, telegram_api_hash)

【讨论】:

    猜你喜欢
    • 2022-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-19
    • 1970-01-01
    相关资源
    最近更新 更多