Python电报机器人-发送图像
我想根据请求发送一个图像(通过URL或路径)。 我使用源代码 代码已经有了一个发送图像的示例(通过URL或路径),但是我没有得到它,因为我是Python新手 以下是示例代码片段:Python电报机器人-发送图像,python,image,api,telegram-bot,Python,Image,Api,Telegram Bot,我想根据请求发送一个图像(通过URL或路径)。 我使用源代码 代码已经有了一个发送图像的示例(通过URL或路径),但是我没有得到它,因为我是Python新手 以下是示例代码片段: elif text == '/image': #request img = Image.new('RGB', (512, 512)) base = random.randint(0, 16777216) pixels = [base+i*j for i in range(512)
elif text == '/image': #request
img = Image.new('RGB', (512, 512))
base = random.randint(0, 16777216)
pixels = [base+i*j for i in range(512) for j in range(512)] # generate sample image
img.putdata(pixels)
output = StringIO.StringIO()
img.save(output, 'JPEG')
reply(img=output.getvalue())
一些API信息可以是
感谢您的耐心。要从URL发送照片,请执行以下操作:
bot.send_photo(chat_id=chat_id, photo='https://telegram.org/img/t_logo.png')
要从本地驱动器发送照片,请执行以下操作:
bot.send_photo(chat_id=chat_id, photo=open('tests/test.png', 'rb'))
是参考文档。我正努力将
python电报机器人
示例与上述建议联系起来。特别是,当有上下文
和更新
时,我找不到聊天ID
和机器人
。现在,我的两分钱:
pic=os.path.expanduser(“~/a.png”)
context.bot.send_photo(chat_id=update.effective_chat.id,photo=open(pic,'rb'))
请看我的回答:我也是python新手,因此可能有人可以在那里改进我的代码。Oof+1是迄今为止python Telegrame唯一可以理解的清晰示例-bot@RaahimFareed很高兴这有帮助:)