在Heroku上使用Telegram Bot发送图像错误



我在部署一个电报机器人到heroku时一直遇到这个问题,想知道你们是否有人可以帮助我。

我试图使用"bot.send_photo"发送图像函数,但它不起作用。我在本地尝试过,它很好,但是当我部署时,它只是不显示任何错误日志,但也不发送图像。我在编程方面有点像个乞丐,所以很抱歉,如果这太明显了。下面是我使用的代码示例和来自heroku的日志:

import requests
import telebot
import os
from flask import Flask, request
from credentials import TOKEN
token = TOKEN
img_link = 'https://www.tarotcardmeanings.net/images/tarotcards-large/tarot-wands-11.jpg'
bot = telebot.TeleBot(token)
server = Flask(__name__)
@bot.message_handler(commands=['start'])
def greet(msg):
user = msg.chat.id
bot.send_message(user, 'Test message')
@bot.message_handler(commands=['image'])
def test(msg):
user = msg.chat.id
bot.send_photo(user, photo = img_link, caption = 'Test caption')
@server.route('/' + token, methods=['POST'])
def getMessage():
bot.process_new_updates([telebot.types.Update.de_json(request.stream.read().decode("utf-8"))])
return "!", 200
@server.route("/")
def webhook():
bot.remove_webhook()
bot.set_webhook(url='https://young-dawn-12665.herokuapp.com/' + token)
return "!", 200

if __name__ == "__main__":
server.run(host="0.0.0.0", port=int(os.environ.get('PORT', 5000)))

Heroku logs:
2021-02-15T17:36:53.000000+00:00 app[api]: Build started by user 
2021-02-15T17:37:35.322092+00:00 heroku[web.1]: Restarting
2021-02-15T17:37:35.338479+00:00 heroku[web.1]: State changed from up to starting
2021-02-15T17:37:35.158049+00:00 app[api]: Deploy aa954a21 by user 
2021-02-15T17:37:35.158049+00:00 app[api]: Release v35 created by user 
2021-02-15T17:37:36.832826+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2021-02-15T17:37:37.043632+00:00 heroku[web.1]: Process exited with status 143
2021-02-15T17:37:42.154525+00:00 heroku[web.1]: Starting process with command `python3 bot.py`
2021-02-15T17:37:47.876470+00:00 heroku[web.1]: State changed from starting to up
2021-02-15T17:37:47.800539+00:00 app[web.1]: * Serving Flask app "bot" (lazy loading)
2021-02-15T17:37:47.800596+00:00 app[web.1]: * Environment: production
2021-02-15T17:37:47.800602+00:00 app[web.1]: WARNING: This is a development server. Do not use it in a production deployment.
2021-02-15T17:37:47.800606+00:00 app[web.1]: Use a production WSGI server instead.
2021-02-15T17:37:47.800721+00:00 app[web.1]: * Debug mode: off
2021-02-15T17:37:47.802021+00:00 app[web.1]: * Running on http://0.0.0.0:54384/ (Press CTRL+C to quit)
2021-02-15T17:37:53.000000+00:00 app[api]: Build succeeded
2021-02-15T17:37:57.145983+00:00 heroku[router]: at=info method=POST path="/1543961103:AAGPnuf8Wt2b1m9T366nY4MzQOnIizLUUxE" host=young-dawn-12665.herokuapp.com request_id=388d5053-0f51-4c2b-a203-f770b3065f22 fwd="91.108.6.48" dyno=web.1 connect=1ms service=6ms status=200 bytes=154 protocol=https
2021-02-15T17:37:57.144789+00:00 app[web.1]: 10.30.238.92 - - [15/Feb/2021 14:37:57] "POST /1543961103:AAGPnuf8Wt2b1m9T366nY4MzQOnIizLUUxE HTTP/1.1" 200 -
2021-02-15T17:38:01.563181+00:00 app[web.1]: 10.30.238.92 - - [15/Feb/2021 14:38:01] "POST /1543961103:AAGPnuf8Wt2b1m9T366nY4MzQOnIizLUUxE HTTP/1.1" 200 -
2021-02-15T17:38:01.563597+00:00 heroku[router]: at=info method=POST path="/1543961103:AAGPnuf8Wt2b1m9T366nY4MzQOnIizLUUxE" host=young-dawn-12665.herokuapp.com request_id=53e87054-6370-4f5d-8e59-cf980f8622d8 fwd="91.108.6.48" dyno=web.1 connect=1ms service=3ms status=200 bytes=154 protocol=https

提前感谢你花时间阅读这篇文章,我真的很感激。

将url传递给send_photo将不起作用。你可以把它作为BytesIO对象传递:

from urllib.request import urlopen
from io import BytesIO
@bot.message_handler(commands=['image'])
def test(msg):
user = msg.chat.id
bot.send_photo(user, photo = BytesIO(urlopen(img_link).read()), caption = 'Test caption')

最新更新