电报错误的URL主机

时间:2018-07-10 06:48:09

标签: python python-3.x telegram telegram-bot python-telegram-bot

我正在制造机器人。我尝试了所有事情,这是我的代码:

def send_photo(chat_id, location , reply_markup=None):
    url = URL + "sendPhoto?chat_id={}&photo={}".format(chat_id,open('1.jpg', 'rb'))
    if reply_markup:
        url += "&reply_markup={}".format(reply_markup)
    print(get_url(url))
    get_url(url)

我的文件位于.py文件夹中,我仔细检查了所有内容,甚至使用了照片电报ID和url,但仍然得到:

{"ok":false,"error_code":400,"description":"Bad Request: wrong URL host"}

2 个答案:

答案 0 :(得分:1)

https://core.telegram.org/bots/api#sendphoto

fun RecyclerView.smoothSnapToPosition(position: Int, snapMode: Int = LinearSmoothScroller.SNAP_TO_START) {
        val smoothScroller = object : LinearSmoothScroller(this.context) {
            override fun getVerticalSnapPreference(): Int = snapMode
            override fun getHorizontalSnapPreference(): Int = snapMode
        }
        smoothScroller.targetPosition = position
        layoutManager?.startSmoothScroll(smoothScroller)
    }

这是输出

import requests
import json

token = 'Token'

def send_photo(chat_id, photo, caption='', parse_mode=None, disable_notification=False, reply_to_message_id=0, reply_markup=None):
    with open(photo, 'rb') as file:
        response = requests.post(
            'https://api.telegram.org/bot{token}/sendPhoto?'.format(token=token),
            data={
                'chat_id':chat_id, #Integer or String
                'caption':caption, #String
                'parse_mode': parse_mode, #String https://core.telegram.org/bots/api#formatting-options
                'disable_notification': disable_notification, #Boolean
                'reply_to_message_id': reply_to_message_id, #Integer
                'reply_markup': json.dumps(reply_markup) if reply_markup is not None else reply_markup, #List
            },
            files={
                'photo': file.read()
            }
        )
        file.close()
        if response.status_code == 200:
            return json.loads(response.text)

reply_markup = {
    'inline_keyboard':[
        [
            {'text':'Hello Milad', 'url':'https://t.me/PythonIRI'}
        ]
    ]
}
print(send_photo('802959264', 'test.png', caption='caption', reply_markup=reply_markup))

答案 1 :(得分:0)

请使用http网址代替本地网址, 它将其他工作 首先上传文件,然后获取file_id 然后将该文件ID发送给机器人。