7

I have seen New API for bots are enabled to create custome bots,I have seen some sources such as this and this I have also read about @fatherbot which is about registering bots,I also searched about some examples about telegram bots such as this one,I know how write codes in php and python but can not find out how to call api methods and where to get start.Does any one has any idea how to get start?

4

7 回答 7

11

你可以使用这个基本的例子来帮助你。我建议使用类似 curl 的方式添加更多修饰并添加一些错误处理。

<?php

$bot_id = "<bot ID generated by BotFather>";

# Note: you want to change the offset based on the last update_id you received
$url = 'https://api.telegram.org/bot' . $bot_id . '/getUpdates?offset=0';
$result = file_get_contents($url);
$result = json_decode($result, true);

foreach ($result['result'] as $message) {
    var_dump($message);
}

# You can send a message like this:
# The chat_id variable will be provided in the getUpdates result
# TODO: urlencode your message
$url = 'https://api.telegram.org/bot' . $bot_id . '/sendMessage?text=message&chat_id=0';
$result = file_get_contents($url);
$result = json_decode($result, true);

var_dump($result['result']);
于 2015-06-27T15:12:37.880 回答
4

根据官方机器人 API

Getting updates

There are two mutually exclusive ways of receiving updates for your bot 
— the getUpdates method on one hand and Webhooks on the other.

所以 PHP bot 脚本通过接收模式以不同的方式工作

使用 getUpdates

bot API 的访问是通过 HTTP GET/POST,官方帮助中有详细说明。

  • 使用无限循环从电报中读取消息,使用 HTTP GET/POST
  • 如果有新消息

    • 解析消息
    • 使用 HTTP GET/POST 发送消息
    • 睡几秒

使用 WebHook

当使用 WebHook(并且配置良好)时,发送到您的机器人的新消息将触发从电报服务器到您配置的 url 的 HTTP POST 请求,在您自己的服务器上,由您的 PHP 脚本解析。

在您的 PHP 脚本中,解析来自 HTTP POST 的新消息,并使用 HTTP POST 将消息发送回电报服务器。


因此,差异仅在从电报获取消息时存在,所有发送到电报的响应都是通过 HTTP GET/POST,详细信息请参阅使请求部分在官方 API 中。

有人在 github 上疯了一些非官方的 PHP api:

于 2015-06-27T13:24:27.057 回答
3

你可以将我的新库用于电报的 bot api! https://github.com/tekook/TelegramLibrary

它具有新 api 的所有功能,是一个易于使用和基于事件的库!

玩得开心!

于 2015-06-30T09:56:07.993 回答
3

我建议初学者以这种方式开始:

  1. 在 Telegram 应用上搜索BotFather

  2. 向他发送/newbot命令。听从他的指示。

  3. 他会给你一个令牌,比如123456789:ABCDefGHIJKLmnopQRstUVwXYz

  4. 打开浏览器窗口,在地址栏上输入以下形式的内容:https://api.telegram.org/bot<token>/getMe
    例如,使用上面的假令牌:https://api.telegram.org/bot123456789:ABCDefGHIJKLmnopQRstUVwXYz/getMe
    它应该以 JSON 格式返回您的机器人信息。这表明访问 Bot API 只不过是发出 HTTP 请求。

  5. 在 Telegram 应用上搜索您的机器人。给它发消息。

  6. 在浏览器窗口中,输入:https://api.telegram.org/bot<token>/getUpdates
    记住替换令牌。您应该会看到刚刚发送的消息。注意fromandchat字段。那是你。

  7. 然后,您可以尝试一些库。为了在这里平衡语言,我建议使用我创建的 Python 框架telepot 。项目页面有很多文档和示例。

最后,即使有库的帮助,我也鼓励您阅读底层Bot API 文档。理解它可以帮助你充分利用它的力量。

祝你好运。

于 2015-10-19T14:21:21.910 回答
1

关于 getUpdates API 和无限循环,php 服务器不能让代码执行超过 30 秒。,所以无限循环不能正常工作。

于 2015-07-02T03:14:57.530 回答
0

我对 Telegram API 也很陌生,但你可以从访问这个 URL 开始,你应该用你自己生成的令牌替换 (token) 购买 BotFather:

https://api.telegram.org/bot(token)/METHOD_NAME

例如,如果你想开始处理 PHP 脚本发送给你的机器人的请求,你应该调用这个:

https://api.telegram.org/bot(token)/setWebhook?url=https://yourdomain.com/path_to_your_script/

请注意,您必须拥有启用 SSL 的网站才能开始使用电报 API。

于 2015-06-25T19:24:12.913 回答
0

作为脚本无法运行超过 30 秒的答案:

使用 set_time_limit(0); 让它永远持续下去。但是请注意,任何无限时间循环都有些危险;诸如 cpu hogs 或内存泄漏之类的副作用会吞噬您的服务器。这就是为什么许多 ISP 不允许此设置。

于 2015-08-17T21:34:33.513 回答