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. Incoming updates are stored on the server until the bot receives them either way, but they will not be kept longer than 24 hours.

有两个方法可以 Bot 接收更新,一个是 getUpdates,另一个是 Webhooks。这里我只用到 getUpdates 获取自己的 Chat ID。暂时没有让 Bot 接收信息的计划。

Regardless of which option you choose, you will receive JSON-serialized Update objects as a result.

使用以上方法都会返回 Update JSON 序列。

Update

This object represents an incoming update. At most one of the optional parameters can be present in any given update.

Field Type Description
update_id Integer The update‘s unique identifier. Update identifiers start from a certain positive number and increase sequentially. This ID becomes especially handy if you’re using Webhooks, since it allows you to ignore repeated updates or to restore the correct update sequence, should they get out of order. If there are no new updates for at least a week, then identifier of the next update will be chosen randomly instead of sequentially.
message Message Optional. New incoming message of any kind — text, photo, sticker, etc.
edited_message Message Optional. New version of a message that is known to the bot and was edited
channel_post Message Optional. New incoming channel post of any kind — text, photo, sticker, etc.
edited_channel_post Message Optional. New version of a channel post that is known to the bot and was edited
inline_query InlineQuery Optional. New incoming inline query
chosen_inline_result ChosenInlineResult Optional. The result of an inline query that was chosen by a user and sent to their chat partner. Please see our documentation on the feedback collecting for details on how to enable these updates for your bot.
callback_query CallbackQuery Optional. New incoming callback query
shipping_query ShippingQuery Optional. New incoming shipping query. Only for invoices with flexible price
pre_checkout_query PreCheckoutQuery Optional. New incoming pre-checkout query. Contains full information about checkout
poll Poll Optional. New poll state. Bots receive only updates about stopped polls and polls, which are sent by the bot

getMe

A simple method for testing your bot’s auth token. Requires no parameters. Returns basic information about the bot in form of a User object.

用以测试 Token 是否有效,会返回一个基本信息的 JSON 序列。

sendMessage

Use this method to send text messages. On success, the sent Message is returned.

私以为,发送信息使用 POST 方法,提交 JSON 是比较方便的,文本内容和关键字编写比较方便。

Parameter Type Required Description
chat_id Integer or String Yes Unique identifier for the target chat or username of the target channel (in the format @channelusername)
text String Yes Text of the message to be sent
parse_mode String Optional Send Markdown or HTML, if you want Telegram apps to show bold, italic, fixed-width text or inline URLs in your bot’s message.
disable_web_page_preview Boolean Optional Disables link previews for links in this message
disable_notification Boolean Optional Sends the message silently. Users will receive a notification with no sound.
reply_to_message_id Integer Optional If the message is a reply, ID of the original message
reply_markup InlineKeyboardMarkup or ReplyKeyboardMarkup or ReplyKeyboardRemove or ForceReply Optional Additional interface options. A JSON-serialized object for an inline keyboard, custom reply keyboard, instructions to remove reply keyboard or to force a reply from the user.

实践

1. 推送 WAN IP

  • 获取 IP
curl -s ipinfo.io/ip
或
wget -qO- -t1 -T2 members.3322.org/dyndns/getip
或
curl cip.ip
  • 推送
curl -s "https://api.telegram.org/bot<TOKEN>/sendMessage?chat_id=CHATID&text=${IP}"

2. 推送日志

curl -d '{"chat_id":CHATID, "text":"'"${LOG}"'", "parse_mode": "Markdown"}' -H "Content-Type: application/json" -X POST https://api.telegram.org/bot<TOKEN>/sendMessage

这里 curl -d 使用的是单引号,Shell 会忽略任何引用值,即屏蔽的单引号内的特殊字符(${LOG})的原本含义,所以需要使用使用 "'"

另一种方式是使用 "\,颇为麻烦:

-d "{\"chat_id\":CHATID, \"text\":\"${LOG}\", \"parse_mode\": \"Markdown\"}"

3. 发送按钮

curl - d '{"chat_id":CHATID, "text":"test text", "reply_markup": {"inline_keyboard": [[{"text":"TEST", "url": "https://telegram.org"}]]} }' - H "Content-Type: application/json" - X POST https: //api.telegram.org/bot<TOKEN>/sendMessage
  • JSON
{
    "chat_id": CHATID,
    "text": "test text",
    "reply_markup":
    {
        "inline_keyboard": [
            [
            {
                "text": "TEST",
                "url": "https://telegram.org"
            }]
        ]
    }
}

curl 提交 json 数据中引用 Shell 变量问题

Unix Shell 中单引号、双引号字符、反斜杠、反引号的使用

Telegram Bot API

https://www.bennythink.com/tgbot0.html/comment-page-1