简体   繁体   English

在Heroku上运行Telethon

[英]Running Telethon on Heroku

I'm trying to make an application which would run permanently (ie send me information every minute to my Telethon account), and figured out the best way to do so would be through hosting it on heroku. 我正在尝试创建一个永久运行的应用程序(即每分钟向我的Telethon帐户发送信息),并想出最好的方法是通过在heroku上托管它。 I'm also using Flask - and am a beginner in all three tools. 我也在使用Flask - 我是这三种工具的初学者。 This is what I have: 这就是我所拥有的:

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()

The thing is that this code creates two errors and I don't understand why. 问题是这段代码会产生两个错误,我不明白为什么。 1. telethon.errors.rpc_errors_303.PhoneMigrateError: (PhoneMigrateError(...), 'The phone number a user is tryin use for authorization is associated with DC 4.') 2. telethon.errors.rpc_errors_420.FloodWaitError: (FloodWaitError(...), 'A wait of 68533 seconds is required') 1. telethon.errors.rpc_errors_303.PhoneMigrateError :( PhoneMigrateError(...),'用户尝试用于授权的电话号码与DC 4相关联。')2。telethon.errors.rpc_errors_420.FloodWaitError:(FloodWaitError( ...),'需要等待68533秒')

I have no idea how to resolve both of these, especially since the script works when run independently, and through a local server. 我不知道如何解决这两个问题,特别是因为脚本在独立运行时以及通过本地服务器运行。 The problem is when trying to bring in heroku. 问题是当试图引入heroku时。

Thanks! 谢谢!

Full error listing: 完整错误列表:

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.')

I thought this way of running it on Heroku was a bit messy. 我认为这种在Heroku上运行它的方式有点乱。 Instead I've created an AlchemySession so that Telethon stores its data into the Heroku Postgres database. 相反,我创建了一个AlchemySession,以便Telethon将其数据存储到Heroku Postgres数据库中。

You can either retrieve the Heroku DATABASE_URL or paste it together yourself and pass that into the AlchemySessionContainer . 您可以自己检索Heroku DATABASE_URL或将其粘贴在一起并将其传递给AlchemySessionContainer There was a bug that prevented this from working but that has just been fixed. 有一个错误阻止了它的工作,但刚刚修复。

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

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