如何使用电报API在我的机器人上发送图像?

时间:2019-09-30 05:59:41

标签: python-3.x telegram-bot

有人可以告诉我如何使用python中的请求库从互联网发送照片吗?

我目前正尝试将照片发送为:

buildscript {
    ext.kotlin_version = '1.2.71'
    repositories {
        google()
        jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.1'
        classpath 'com.google.gms:google-services:4.3.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        //classpath 'com.google.gms:google-services:4.3.2'
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

rootProject.buildDir = '../build'
subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
    project.evaluationDependsOn(':app')
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

提前谢谢

1 个答案:

答案 0 :(得分:1)

您传递了错误的网址参数。

使用sendPhoto-Fchat_id?符号代替&。如果您想添加多个参数;看看Querystring Array Parameters in Python using Requests

sendPhoto?chat_id=123&photo=somelink

要回答问题,您可以使用以下请求库:

import requests
from requests.exceptions import HTTPError

url="https://api.telegram.org/bot<MY-TOKEN>/sendPhoto?chat_id=<MY-CHAT-ID>&photo=http://via.placeholder.com/100x100"

try:
    response = requests.get(url)

    # If the response was successful, no Exception will be raised
    response.raise_for_status()
except HTTPError as http_err:
    print(f'HTTP error occurred: {http_err}')  # Python 3.6
except Exception as err:
    print(f'Other error occurred: {err}')  # Python 3.6
else:
    print('Success!')