我们应该如何将查询发送到Telegram机器人API? [英] How we should send query to Telegram bot API?
问题描述
创建电报机器人并获取机器人令牌后,我想向机器人API发送请求.
After creating a telegram bot and gain bot token, I want to send a request to the bot API.
此链接说,我们必须像这样发送HTTP请求:
https://api.telegram.org/bot<token>/METHOD_NAME
并提供了最简单的方法"getme"的示例,该方法没有任何输入参数.
This link says we must send the HTTP request like this:
https://api.telegram.org/bot<token>/METHOD_NAME
and brings example for easiest method "getme" which has not any input parameters.
想象一下我想发送一些消息.我应该使用sendMessage
方法,该方法具有两个必需的输入参数:chat_ID和text.
Imagine I want to send some messages. I should use the sendMessage
method which has two Required input parameters: chat_ID and text.
现在我的问题开始了:
-
如何以上述请求格式及其参数编写sendMessage方法?我尝试了
sendMessage(param1,param2)
并收到未找到方法的消息.
How can I write this sendMessage method in above request format with its parameters? I tried
sendMessage(param1,param2)
and received method not found message.
什么是chat_id
?如果我想向联系人发送消息,我怎么知道他的chat_id
?
What is chat_id
? if I want to send a message to the contact, how can I know his chat_id
?
我在互联网上搜索了很多东西,GitHub上有很多专门用于此目的的项目,说实话,这些项目都没有任何意义. 看在上帝的份上,请帮助我.我很迷路.
I searched a lot on the internet, there are plenty of projects on GitHub especially for this purpose, and honestly none of them makes any sense. for god's sake someone please help me. I am loosing way.
致谢.
推荐答案
您只需将POST请求发送至:
You just send a POST request to:
https://api.telegram.org/bot{token}/{method}
例如:
https://api.telegram.org/bot123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11/sendMessage
在请求的正文中,您对参数进行URL编码:
In the body of the request, you URL encode the parameters:
chat_id=12345&text=hello%20friend
例如,在Python中使用requests
模块:
For example, in Python using the requests
module:
import requests
response = requests.post(
url='https://api.telegram.org/bot{0}/{1}'.format(token, method),
data={'chat_id': 12345, 'text': 'hello friend'}
).json()
当用户与您的漫游器聊天时,您会得到一个 Message
对象具有一个聊天ID(和一个用户ID,您可以用它代替聊天ID).除非您已经知道他们的用户ID,否则无法与该用户发起聊天,因此您必须等待用户与您交谈.您可以使用深度链接并让用户单击链接来简化此操作当他们按下开始"按钮时,会发送预制消息.
When a user chats with your bot, you get a Message
object that has a chat id (and a user id, which you can substitute for a chat id). There's no way to initiate a chat with a user unless you already know their user id, so you have to wait for a user to talk to you. You can simplify that by using deep linking and having the user click on a link that sends a pre-made message when they hit the Start button.
对于那些努力寻找chat_id的人,这是一种方法:
for those struggling to find chat_id, here's a way:
1.- 创建机器人:在Telegram的搜索中查找 @BotFather .单击开始,编写/newbot,并为其命名和用户名.您应该获得一个令牌来访问HTTP API.保存此令牌.
1.- Create a bot: on Telegram's search look for @BotFather. Click start, write /newbot, give it a name and a username. You should get a token to access the HTTP API. Save this token.
2.- 在Telegram上使用其用户名找到您的机器人.给它写一些东西,例如'测试'.稍后会派上用场.
2.- Find your bot on Telegram with its username. Write something to it e.g. 'test'. This will come in handy later.
3.- 打印chat_id .在运行此功能之前,请确保至少已在Telegram上向机器人写入了一条消息(第2步)
3.- Print chat_id. Before running this function, make sure that you have at least written one message to your bot on Telegram (step 2)
JavaScript代码:
Javascript code:
var token = "123456:kioASDdjicOljd_ijsdf"; // Fill this in with your token
var telegramUrl = "https://api.telegram.org/bot" + token;
function getChat_id(){
var res = UrlFetchApp.fetch(telegramUrl+"/getUpdates").getContentText();
var res = JSON.parse(res);
Logger.log(res.result[0].message.chat.id.toString());
}
这篇关于我们应该如何将查询发送到Telegram机器人API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!