3

我正在尝试制作一个可以永久运行的应用程序(即每分钟向我的 Telethon 帐户发送信息),并想出最好的方法是将其托管在 heroku 上。我也在使用 Flask - 并且是所有三种工具的初学者。这就是我所拥有的:

from twx.botapi import TelegramBot, ReplyKeyboardMarkup
from telethon import TelegramClient
from telethon.tl.types.input_peer_chat import InputPeerChat
import time
from flask import Flask, render_template, request, redirect

app = Flask(__name__)
chat_id = 40398****
api_id = '10****'
api_hash = 'bae9b**********************'

client = TelegramClient('ID_1', api_id=api_id, api_hash=api_hash)
client.connect()
chat = InputPeerChat(chat_id)

@app.route('/')
def starting_page():
    global client

    if not client.is_user_authorized():
        client.send_code_request('+1234567890') # this is a phone number

    return render_template('index.html', author=author)
.
.
.
#if __name__ == '__main__': # this seems to run automatically on heroku so I commented it out
#    app.run()

问题是这段代码会产生两个错误,我不明白为什么。1. telethon.errors.rpc_errors_303.PhoneMigrateError: (PhoneMigrateError(...), '用户尝试用于授权的电话号码与 DC 4 相关联。') 2. telethon.errors.rpc_errors_420.FloodWaitError: (FloodWaitError( ...), '需要等待 68533 秒')

我不知道如何解决这两个问题,特别是因为脚本在独立运行并通过本地服务器运行时有效。问题是在尝试引入 heroku 时。

谢谢!

完整的错误列表:

2017-08-19T13:00:39.644337+00:00 app[web.1]: [2017-08-19 13:00:39,641] ERROR in app: Exception on / [GET]
2017-08-19T13:00:39.644346+00:00 app[web.1]: Traceback (most recent call last):
2017-08-19T13:00:39.644349+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/telethon/telegram_client.py", line 205, in invoke
2017-08-19T13:00:39.644350+00:00 app[web.1]:     request, updates=updates
2017-08-19T13:00:39.644351+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/telethon/telegram_bare_client.py", line 276, in invo
2017-08-19T13:00:39.644352+00:00 app[web.1]:     self._sender.receive(request, updates=updates)
2017-08-19T13:00:39.644352+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/telethon/network/mtproto_sender.py", line 109, in ree
2017-08-19T13:00:39.644354+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/telethon/network/mtproto_sender.py", line 207, in _pss_msg
2017-08-19T13:00:39.644354+00:00 app[web.1]:     return self._handle_rpc_result(msg_id, sequence, reader)
2017-08-19T13:00:39.644353+00:00 app[web.1]:     remote_msg_id, remote_seq, reader, updates)
2017-08-19T13:00:39.644356+00:00 app[web.1]:     raise error
2017-08-19T13:00:39.644357+00:00 app[web.1]: telethon.errors.rpc_errors_303.PhoneMigrateError: (PhoneMigrateError(...), 'The phone number a user is tryin use for authorization is associated with DC 4.')
2017-08-19T13:00:39.644358+00:00 app[web.1]:
2017-08-19T13:00:39.644355+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/telethon/network/mtproto_sender.py", line 355, in _he_rpc_result
2017-08-19T13:00:39.644359+00:00 app[web.1]: During handling of the above exception, another exception occurred:
2017-08-19T13:00:39.644359+00:00 app[web.1]:
2017-08-19T13:00:39.644360+00:00 app[web.1]: Traceback (most recent call last):
2017-08-19T13:00:39.644360+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1982, in wsgi_app
2017-08-19T13:00:39.644361+00:00 app[web.1]:     response = self.full_dispatch_request()
2017-08-19T13:00:39.644362+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1614, in full_dispatch_request
2017-08-19T13:00:39.644362+00:00 app[web.1]:     rv = self.handle_user_exception(e)
2017-08-19T13:00:39.644363+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1517, in handle_user_exception
2017-08-19T13:00:39.644363+00:00 app[web.1]:     reraise(exc_type, exc_value, tb)
2017-08-19T13:00:39.644364+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
2017-08-19T13:00:39.644365+00:00 app[web.1]:     raise value
2017-08-19T13:00:39.644365+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1612, in full_dispatch_request
2017-08-19T13:00:39.644366+00:00 app[web.1]:     rv = self.dispatch_request()
2017-08-19T13:00:39.644367+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1598, in dispatch_request
2017-08-19T13:00:39.644367+00:00 app[web.1]:     return self.view_functions[rule.endpoint](**req.view_args)
2017-08-19T13:00:39.644368+00:00 app[web.1]:   File "/app/telegram_bot_working.py", line 33, in starting_page
2017-08-19T13:00:39.644368+00:00 app[web.1]:     client.send_code_request('+1234567890')
2017-08-19T13:00:39.644369+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/telethon/telegram_client.py", line 259, in send_codeuest
2017-08-19T13:00:39.644370+00:00 app[web.1]:     SendCodeRequest(phone_number, self.api_id, self.api_hash))
2017-08-19T13:00:39.644370+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/telethon/telegram_client.py", line 222, in invoke
2017-08-19T13:00:39.644371+00:00 app[web.1]:     return self.invoke(request)
2017-08-19T13:00:39.644372+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/telethon/telegram_client.py", line 205, in invoke
2017-08-19T13:00:39.644372+00:00 app[web.1]:     request, updates=updates
2017-08-19T13:00:39.644373+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/telethon/telegram_bare_client.py", line 276, in invo
2017-08-19T13:00:39.644374+00:00 app[web.1]:     self._sender.receive(request, updates=updates)
2017-08-19T13:00:39.644375+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/telethon/network/mtproto_sender.py", line 109, in ree
2017-08-19T13:00:39.644375+00:00 app[web.1]:     remote_msg_id, remote_seq, reader, updates)
2017-08-19T13:00:39.644376+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/telethon/network/mtproto_sender.py", line 207, in _pss_msg
2017-08-19T13:00:39.644376+00:00 app[web.1]:     return self._handle_rpc_result(msg_id, sequence, reader)
2017-08-19T13:00:39.644377+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/telethon/network/mtproto_sender.py", line 355, in _he_rpc_result
2017-08-19T13:00:39.644378+00:00 app[web.1]:     raise error
2017-08-19T13:00:39.644383+00:00 app[web.1]: telethon.errors.rpc_errors_420.FloodWaitError: (FloodWaitError(...), 'A wait of 68533 seconds is required.')
4

1 回答 1

0

我认为这种在 Heroku 上运行它的方式有点混乱。相反,我创建了一个AlchemySession,以便 Telethon 将其数据存储到 Heroku Postgres 数据库中。

您可以检索 HerokuDATABASE_URL或自己将其粘贴在一起并将其传递到AlchemySessionContainer. 有一个错误阻止了它的工作,但刚刚修复。

于 2018-03-06T12:23:44.723 回答