如何向 Telegram bot API 发送请求? [英] How to send request to Telegram bot API?

查看:108
本文介绍了如何向 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.

现在我的问题开始:

  1. 如何用上面的请求格式编写这个sendMessage方法及其参数?我尝试了 sendMessage(param1,param2) 并收到方法未找到消息.

  1. 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 bot API 发送请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆