簡體   English   中英

在 Heroku 上使用 Telegram Bot 發送圖像時出錯

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

我在將電報機器人部署到 heroku 時遇到了這個問題,想知道你們是否可以提供幫助。

我嘗試使用“bot.send_photo”function 發送圖像,但它不起作用。 我在本地嘗試過,很好,但是當我部署它時,它沒有顯示任何錯誤日志,但也沒有發送圖像。 如果編程,我有點像初學者,如果這太明顯了,很抱歉。 這是我正在使用的代碼示例以及來自 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 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