gpt4 book ai didi

python - 通过 python 向 Telegram Bot 发送文本消息

转载 作者:行者123 更新时间:2023-12-05 02:05:48 59 4
gpt4 key购买 nike

我从早上开始就一直在尝试,但早些时候出现了错误,所以我有了方向,但现在没有错误,甚至也没有警告..

代码的样子:

import requests

def send_msg(text):
token = "TOKEN"
chat_id = "CHATID"
url_req = "https://api.telegram.org/bot" + token + "/sendMessage" + "?chat_id=" + chat_id + "&text=" + text
results = requests.get(url_req)
print(results.json())

send_msg("hi there 1234")

预期输出是什么:它应该发送一条短信

什么是当前输出:它什么都不打印

如果有人帮忙就太好了,谢谢大家

编辑:2

由于未安装以下依赖项,因此无法发送文本。

$ pip install flask
$ pip install python-telegram-bot
$ pip install requests

现在有人可以帮我发送照片吗?我认为它不能通过 URL 发送图像,谢谢大家

**编辑 3 **

我从here 找到了图片或视频分享网址但是我的图片是本地的,不是来自远程服务器

最佳答案

您的代码没有任何问题。您需要做的就是适当的缩进。

This error primarily occurs because there are space or tab errors inyour code. Since Python uses procedural language, you may experiencethis error if you have not placed the tabs/spaces correctly.

运行以下代码。它会正常工作:

import requests

def send_msg(text):
token = "your_token"
chat_id = "your_chatId"
url_req = "https://api.telegram.org/bot" + token + "/sendMessage" + "?chat_id=" + chat_id + "&text=" + text
results = requests.get(url_req)
print(results.json())

send_msg("Hello there!")

使用机器人库发送图片可能更容易:bot.sendPhoto(chat_id, 'URL')

注意:最好将编辑器配置为使制表符和空格可见以避免此类错误。

关于python - 通过 python 向 Telegram Bot 发送文本消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63103077/

59 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com