简体   繁体   English

Python:发送照片电报机器人时出现问题

[英]Python: Problem with send photo telegram bot

I learn to make a telegram bot with python using the python-bot-telegram package, but I have trouble with sending photo when I type something, when I try to print, it shows an error that there is no photo in this request .我学会了使用python-bot-telegram package 制作带有 python 的电报机器人,但是当我输入内容时发送照片时遇到问题,当我尝试打印时,它显示一个错误,即there is no photo in this request sometimes give me an error Wrong remote file identifier specified: can't unserialize it. wrong last symbol有时会给我一个错误Wrong remote file identifier specified: can't unserialize it. wrong last symbol Wrong remote file identifier specified: can't unserialize it. wrong last symbol . Wrong remote file identifier specified: can't unserialize it. wrong last symbol

starting up bot
https://ews.bmkg.go.id/tews/data/20221005051529.mmi.jpg
there is no photo in this request
starting up bot
https://ews.bmkg.go.id/tews/data/20221005051529.mmi.jpg
Wrong remote file identifier specified: can't unserialize it. wrong last symbol

I'm confused because the print show there is a link as you can see on the log up there, also there is an error when I move the method to queryHandler function the error comes like this我很困惑,因为打印显示有一个链接,正如您在上面的日志中看到的那样,当我将方法移动到queryHandler function 时也出现错误错误是这样的

starting up bot
None
'NoneType' object is not subscriptable

script that shows error no photos and wrong file identifier:显示错误的脚本没有照片和错误的文件标识符:

from telegram import *
from telegram.ext import *
from requests import *


imgUrl=None

def getURl(urlImg):
    imgUrl=urlImg
    print(imgUrl)

def handle_message(update:Update,context:CallbackContext):
    text = str(update.message.text).lower()
    response= r.sample_responses(text)

    update.message.reply_text(response)
    if text == "bmkg" or text == "gempa terkini":
        context.bot.send_photo(chat_id=update.effective_chat.id,photo=f"{imgUrl}")
        

but when I change the photo="https://ews.bmkg.go.id/tews/data/20221005051529.mmi.jpg" instead photo = f"{imgUrl}" it worked但是当我更改photo="https://ews.bmkg.go.id/tews/data/20221005051529.mmi.jpg"而不是photo = f"{imgUrl}"它起作用了

script that shows None error:显示 None 错误的脚本:

from telegram import *
from telegram.ext import *
from requests import *


def handle_message(update:Update,context:CallbackContext):
    text = str(update.message.text).lower()
    response= r.sample_responses(text)

    update.message.reply_text(response)
    if text == "bmkg" or text == "gempa terkini":
        buttons=[
            [InlineKeyboardButton("🌎",callback_data="show_image")],
        ]
        context.bot.send_message(chat_id=update.effective_chat.id,reply_markup=InlineKeyboardMarkup(buttons), text="Tampilkan gambar?")

imgUrl=None

def getURl(urlImg):
    imgUrl=urlImg
    print(imgUrl)

def queryHandler(update:Update,context:CallbackContext):
    query = update.callback_query.data
    update.callback_query.answer()

    print(imgUrl)

    gempa_di_indonesia = GempaTerkini()
    urlImg= gempa_di_indonesia.get_img()

    if "show_image" in query:
        context.bot.send_photo(chat_id=update.effective_chat.id,photo=f"{urlImg}")

this is nothing that works, always print None这没什么用,总是打印 None

note: the imgUrl is from an inherited class that returns str注意:imgUrl 来自返回 str 的继承 class

def get_img(self) -> str:
        src_img= str(self.result['src'])
        return src_img

Am I doing it wrong here?我在这里做错了吗?

this is the answer to my question, I don't know if I'm right but, I assumed the code run immediately so the imgUrl is always null or None so I have to learn about async in python这是我的问题的答案,我不知道我是否正确但是,我假设代码立即运行所以imgUrl总是null or None所以我必须在 python 中了解异步

so in this case I have to call from the inheritance class again to get the image URL所以在这种情况下,我必须再次从 inheritance class 调用以获取图像 URL

def handle_message(update:Update,context:CallbackContext):
    text = str(update.message.text).lower()
    response= r.sample_responses(text)

    update.message.reply_text(response)
    if text == "bmkg" or text == "gempa terkini":
        # try:
        #     context.bot.send_photo(chat_id=update.effective_chat.id,photo=f"{r.ImgUrl}")
        # except:
        #     traceback.print_exc()

        buttons=[
            [InlineKeyboardButton("🌎",callback_data="show_image")],
        ]
        context.bot.send_message(chat_id=update.effective_chat.id,reply_markup=InlineKeyboardMarkup(buttons), text="Tampilkan gambar?")


def queryHandler(update:Update,context:CallbackContext):
    try:
        query = update.callback_query.data
        update.callback_query.answer()

        ## call it again from inheritance class
        gempa_di_indonesia = GempaTerkini()
        gempa_di_indonesia.run()
        a = gempa_di_indonesia.get_image()

        if "show_image" in query:
            context.bot.send_photo(chat_id=update.effective_chat.id,photo=f"{a}")

    except:
        traceback.print_exc()

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

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