简体   繁体   English

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

[英]Error sending image with a Telegram Bot on Heroku

I have been having this issue while deploying a telegram bot to heroku and was wondering if any of you could help.我在将电报机器人部署到 heroku 时遇到了这个问题,想知道你们是否可以提供帮助。

I tried to send an image using the "bot.send_photo" function but it doesn't work.我尝试使用“bot.send_photo”function 发送图像,但它不起作用。 I tried locally and it was just fine, but when I deploy it just doesn't show any error logs but doesn't send the image either.我在本地尝试过,很好,但是当我部署它时,它没有显示任何错误日志,但也没有发送图像。 I am somewhat of a begginer if programming, so sorry if this is too obvious.如果编程,我有点像初学者,如果这太明显了,很抱歉。 Here's a sample of the code I am using and the logs from the heroku:这是我正在使用的代码示例以及来自 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

Thanks in advance for the time you took reading this, I really appreciate it a lot.提前感谢您花时间阅读本文,我非常感谢。

Passing an url to send_photo will not work.将 url 传递给send_photo将不起作用。 You can pass it as a BytesIO object:您可以将其作为 BytesIO object 传递:

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

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

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