{"id":166,"date":"2025-11-09T08:42:59","date_gmt":"2025-11-09T00:42:59","guid":{"rendered":"https:\/\/blog.poker-x-studio.com\/?p=166"},"modified":"2025-11-09T08:47:03","modified_gmt":"2025-11-09T00:47:03","slug":"telegram-bot-%e6%a8%a1%e5%9e%8b%e4%b8%8b%ef%bc%9abot-%e6%9c%8d%e5%8a%a1%e5%99%a8%e3%80%81telegram-%e6%9c%8d%e5%8a%a1%e5%99%a8%e4%b8%8e%e7%94%a8%e6%88%b7%e4%b9%8b%e9%97%b4%e7%9a%84%e4%ba%a4%e4%ba%92","status":"publish","type":"post","link":"https:\/\/blog.poker-x-studio.com\/index.php\/2025\/11\/09\/telegram-bot-%e6%a8%a1%e5%9e%8b%e4%b8%8b%ef%bc%9abot-%e6%9c%8d%e5%8a%a1%e5%99%a8%e3%80%81telegram-%e6%9c%8d%e5%8a%a1%e5%99%a8%e4%b8%8e%e7%94%a8%e6%88%b7%e4%b9%8b%e9%97%b4%e7%9a%84%e4%ba%a4%e4%ba%92\/","title":{"rendered":"Telegram Bot \u6a21\u578b\u4e0b\uff1aBot \u670d\u52a1\u5668\u3001Telegram \u670d\u52a1\u5668\u4e0e\u7528\u6237\u4e4b\u95f4\u7684\u4ea4\u4e92\u673a\u5236"},"content":{"rendered":"<h1>\u672c\u7bc7\u6240\u6709\u5185\u5bb9\u7686\u4e3agrok\u56de\u590d<\/h1>\n<blockquote>\n<p><strong>\u6838\u5fc3\u7ed3\u8bba<\/strong>\uff1a<br \/>\n<strong>Bot \u670d\u52a1\u5668\u4e0e\u7528\u6237\u4e4b\u95f4\u65e0\u76f4\u63a5\u901a\u4fe1\uff0c\u6240\u6709\u4ea4\u4e92\u5fc5\u987b\u901a\u8fc7 Telegram \u5b98\u65b9\u670d\u52a1\u5668\u4e2d\u8f6c\u3002<\/strong><\/p>\n<\/blockquote>\n<hr \/>\n<h2>\u4e00\u3001\u6574\u4f53\u67b6\u6784\u6982\u89c8<\/h2>\n<pre><code>\u7528\u6237 (Telegram \u5ba2\u6237\u7aef)\n     &harr; HTTPS\nTelegram \u5b98\u65b9\u670d\u52a1\u5668\n     &harr; HTTPS (TLS \u52a0\u5bc6)\nBot \u670d\u52a1\u5668 (\u4f60\u7684\u4ee3\u7801)<\/code><\/pre>\n<ul>\n<li><strong>\u7528\u6237 \u2194 Telegram<\/strong>\uff1a\u76f4\u63a5\u957f\u8fde\u63a5\uff08WebSocket \/ HTTPS\uff09<\/li>\n<li><strong>Bot \u670d\u52a1\u5668 \u2194 Telegram<\/strong>\uff1a\u901a\u8fc7 Bot API\uff08HTTPS\uff09<\/li>\n<li><strong>Bot \u670d\u52a1\u5668 \u2194 \u7528\u6237<\/strong>\uff1a<strong>\u65e0\u76f4\u63a5\u8fde\u63a5<\/strong><\/li>\n<\/ul>\n<hr \/>\n<h2>\u4e8c\u3001\u4e24\u79cd\u66f4\u65b0\u83b7\u53d6\u6a21\u5f0f<\/h2>\n<table>\n<thead>\n<tr>\n<th>\u6a21\u5f0f<\/th>\n<th>\u8bf4\u660e<\/th>\n<th>\u6570\u636e\u6d41\u5411<\/th>\n<th>\u9002\u7528\u573a\u666f<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><strong>Long Polling\uff08\u8f6e\u8be2\uff09<\/strong><\/td>\n<td>Bot \u4e3b\u52a8\u5411 Telegram \u62c9\u53d6\u66f4\u65b0<\/td>\n<td>Bot \u2192 TG \u2192 Bot<\/td>\n<td>\u5f00\u53d1\u6d4b\u8bd5\u3001\u5185\u7f51\u670d\u52a1\u5668<\/td>\n<\/tr>\n<tr>\n<td><strong>Webhook\uff08\u94a9\u5b50\uff09<\/strong><\/td>\n<td>Telegram \u4e3b\u52a8\u63a8\u9001\u66f4\u65b0\u5230 Bot<\/td>\n<td>TG \u2192 Bot<\/td>\n<td>\u751f\u4ea7\u73af\u5883\uff08\u63a8\u8350\uff09<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr \/>\n<h3>1. Long Polling \u6d41\u7a0b<\/h3>\n<pre><code class=\"lang-mermaid language-mermaid\">sequenceDiagram\n    participant U as \u7528\u6237\n    participant TG as Telegram \u670d\u52a1\u5668\n    participant B as Bot \u670d\u52a1\u5668\n\n    U-&gt;&gt;TG: \u53d1\u9001\u6d88\u606f\n    B-&gt;&gt;TG: getUpdates() [\u8f6e\u8be2]\n    TG--&gt;&gt;B: \u8fd4\u56de Update \u5217\u8868\n    B-&gt;&gt;TG: sendMessage()\n    TG--&gt;&gt;U: \u63a8\u9001\u56de\u590d<\/code><\/pre>\n<ul>\n<li>\u8c03\u7528\uff1a<code>GET \/getUpdates<\/code><\/li>\n<li>\u65e0\u9700\u516c\u7f51\uff0c\u4f46\u6548\u7387\u4f4e\u3001\u5ef6\u8fdf\u9ad8<\/li>\n<\/ul>\n<hr \/>\n<h3>2. Webhook \u6d41\u7a0b\uff08\u63a8\u8350\uff09<\/h3>\n<pre><code class=\"lang-mermaid language-mermaid\">sequenceDiagram\n    participant U as \u7528\u6237\n    participant TG as Telegram \u670d\u52a1\u5668\n    participant B as Bot \u670d\u52a1\u5668\n\n    Note over B: setWebhook(url) \u5df2\u8bbe\u7f6e\n    U-&gt;&gt;TG: \u53d1\u9001\u6d88\u606f\n    TG-&gt;&gt;B: POST \/webhook (JSON Update)\n    B--&gt;&gt;TG: HTTP 200\n    B-&gt;&gt;TG: sendMessage()\n    TG--&gt;&gt;U: \u63a8\u9001\u56de\u590d<\/code><\/pre>\n<ul>\n<li>\u8c03\u7528\uff1a<code>POST \/setWebhook?url=https:\/\/yourbot.com\/webhook<\/code><\/li>\n<li>\u8981\u6c42\uff1a\n<ul>\n<li>\u516c\u7f51 HTTPS\uff08443 \u7aef\u53e3\uff09<\/li>\n<li>\u6709\u6548 SSL \u8bc1\u4e66<\/li>\n<li>\u5feb\u901f\u54cd\u5e94\uff08&lt; \u51e0\u79d2\uff09<\/li>\n<\/ul><\/li>\n<\/ul>\n<hr \/>\n<h2>\u4e09\u3001\u6838\u5fc3\u6570\u636e\uff1aUpdate \u5bf9\u8c61<\/h2>\n<p>Telegram \u63a8\u9001\/\u8fd4\u56de\u7684 JSON \u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"lang-json language-json json\">{\n  &quot;update_id&quot;: 123456789,\n  &quot;message&quot;: {\n    &quot;message_id&quot;: 100,\n    &quot;from&quot;: { &quot;id&quot;: 987654321, &quot;first_name&quot;: &quot;\u5f20\u4e09&quot; },\n    &quot;chat&quot;: { &quot;id&quot;: 987654321, &quot;type&quot;: &quot;private&quot; },\n    &quot;date&quot;: 1731171234,\n    &quot;text&quot;: &quot;\/start&quot;\n  }\n}<\/code><\/pre>\n<p>\u5305\u542b\u7c7b\u578b\uff1a<code>message<\/code>, <code>callback_query<\/code>, <code>inline_query<\/code> \u7b49\u3002<\/p>\n<hr \/>\n<h2>\u56db\u3001Bot \u56de\u590d\u7528\u6237\uff08\u901a\u8fc7 API\uff09<\/h2>\n<p>\u6240\u6709\u56de\u590d\u5fc5\u987b\u8c03\u7528 Bot API\uff1a<\/p>\n<pre><code class=\"lang-http language-http http\">POST https:\/\/api.telegram.org\/bot\n&lt;TOKEN&gt;\/sendMessage\nContent-Type: application\/json\n\n{\n  &quot;chat_id&quot;: 987654321,\n  &quot;text&quot;: &quot;\u4f60\u597d\uff01&quot;\n}<\/code><\/pre>\n<p>\u5e38\u89c1\u65b9\u6cd5\uff1a<\/p>\n<ul>\n<li><code>sendMessage<\/code>\uff1a\u6587\u5b57<\/li>\n<li><code>sendPhoto<\/code>\uff1a\u56fe\u7247<\/li>\n<li><code>editMessageText<\/code>\uff1a\u7f16\u8f91\u6d88\u606f<\/li>\n<li><code>answerCallbackQuery<\/code>\uff1a\u6309\u94ae\u56de\u8c03<\/li>\n<\/ul>\n<hr \/>\n<h2>\u4e94\u3001\u5b89\u5168\u673a\u5236<\/h2>\n<table>\n<thead>\n<tr>\n<th>\u673a\u5236<\/th>\n<th>\u8bf4\u660e<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><strong>Bot Token<\/strong><\/td>\n<td>\u552f\u4e00\u8eab\u4efd\u51ed\u8bc1\uff0c\u7c7b\u4f3c\u5bc6\u7801<\/td>\n<\/tr>\n<tr>\n<td><strong>HTTPS \u5f3a\u5236<\/strong><\/td>\n<td>Webhook \u5fc5\u987b\u4f7f\u7528 TLS<\/td>\n<\/tr>\n<tr>\n<td><strong>Secret Token<\/strong><\/td>\n<td>\u81ea\u5b9a\u4e49\u5934\u9a8c\u8bc1\u6765\u6e90<\/td>\n<\/tr>\n<tr>\n<td><strong>IP \u767d\u540d\u5355<\/strong><\/td>\n<td>\u53ef\u9650\u5236\u4ec5 Telegram IP \u8bbf\u95ee<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<pre><code class=\"lang-python language-python python\"># \u793a\u4f8b\u9a8c\u8bc1\nif request.headers.get('X-Telegram-Bot-Api-Secret-Token') != 'your_secret':\n    abort(403)<\/code><\/pre>\n<hr \/>\n<h2>\u516d\u3001\u5b8c\u6574\u4ea4\u4e92\u56fe\uff08Webhook \u6a21\u5f0f\uff09<\/h2>\n<pre><code class=\"lang-mermaid language-mermaid\">graph TD\n    A[\u7528\u6237\u53d1\u9001\u6d88\u606f] --&gt; B(Telegram \u670d\u52a1\u5668)\n    B --&gt; C{\u751f\u6210 Update}\n    C --&gt; D[POST \u5230 yourbot.com\/webhook]\n    D --&gt; E[Bot \u9a8c\u8bc1 HTTPS + Secret Token]\n    E --&gt; F[\u5904\u7406\u903b\u8f91]\n    F --&gt; G[\u8c03\u7528 sendMessage API]\n    G --&gt; B\n    B --&gt; H[\u63a8\u9001\u7ed9\u7528\u6237]<\/code><\/pre>\n<hr \/>\n<h2>\u4e03\u3001\u5e38\u89c1\u90e8\u7f72\u65b9\u6848<\/h2>\n<table>\n<thead>\n<tr>\n<th>\u5e73\u53f0<\/th>\n<th>Webhook \u652f\u6301<\/th>\n<th>\u63a8\u8350\u5ea6<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>VPS \/ \u4e91\u670d\u52a1\u5668<\/td>\n<td>Yes<\/td>\n<td>5 stars<\/td>\n<\/tr>\n<tr>\n<td>Heroku \/ Render<\/td>\n<td>Yes<\/td>\n<td>4 stars<\/td>\n<\/tr>\n<tr>\n<td>Vercel \/ Cloudflare<\/td>\n<td>Warning (\u9700\u9002\u914d)<\/td>\n<td>3 stars<\/td>\n<\/tr>\n<tr>\n<td>Ngrok<\/td>\n<td>Yes (\u4e34\u65f6)<\/td>\n<td>\u5f00\u53d1\u4e13\u7528<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr \/>\n<h2>\u516b\u3001\u4e3a\u4ec0\u4e48\u4e0d\u80fd\u76f4\u8fde\u7528\u6237\uff1f<\/h2>\n<table>\n<thead>\n<tr>\n<th>\u539f\u56e0<\/th>\n<th>\u8bf4\u660e<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><strong>\u9690\u79c1\u4fdd\u62a4<\/strong><\/td>\n<td>\u4e0d\u66b4\u9732\u7528\u6237 IP\u3001\u8bbe\u5907\u4fe1\u606f<\/td>\n<\/tr>\n<tr>\n<td><strong>\u5b89\u5168\u9694\u79bb<\/strong><\/td>\n<td>Bot \u65e0\u6cd5\u76f4\u63a5\u653b\u51fb\u7528\u6237<\/td>\n<\/tr>\n<tr>\n<td><strong>\u53cd\u5783\u573e<\/strong><\/td>\n<td>\u9650\u5236\u4e3b\u52a8\u53d1\u9001\u6d88\u606f<\/td>\n<\/tr>\n<tr>\n<td><strong>\u7edf\u4e00\u7ba1\u7406<\/strong><\/td>\n<td>Telegram \u53ef\u5c01\u7981\u6076\u610f Bot<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<blockquote>\n<p><strong>\u53ea\u80fd\u7ed9\u4e3b\u52a8\u548c\u4f60\u804a\u8fc7\u7684\u7528\u6237\u53d1\u6d88\u606f<\/strong><\/p>\n<\/blockquote>\n<hr \/>\n<h2>\u4e5d\u3001\u5b98\u65b9\u6587\u6863<\/h2>\n<ul>\n<li>Bot API: <a href=\"https:\/\/core.telegram.org\/bots\/api\">https:\/\/core.telegram.org\/bots\/api<\/a><\/li>\n<li>Webhook: <a href=\"https:\/\/core.telegram.org\/bots\/api#setwebhook\">https:\/\/core.telegram.org\/bots\/api#setwebhook<\/a><\/li>\n<li>getUpdates: <a href=\"https:\/\/core.telegram.org\/bots\/api#getupdates\">https:\/\/core.telegram.org\/bots\/api#getupdates<\/a><\/li>\n<\/ul>\n<hr \/>\n<h2>\u5341\u3001\u603b\u7ed3\u8868<\/h2>\n<table>\n<thead>\n<tr>\n<th>\u89d2\u8272<\/th>\n<th>\u804c\u8d23<\/th>\n<th>\u4e0e\u8c01\u76f4\u63a5\u901a\u4fe1<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><strong>\u7528\u6237<\/strong><\/td>\n<td>\u53d1\u9001\u6d88\u606f<\/td>\n<td>Telegram<\/td>\n<\/tr>\n<tr>\n<td><strong>Telegram \u670d\u52a1\u5668<\/strong><\/td>\n<td>\u4e2d\u8f6c\u3001\u5b58\u50a8\u3001\u63a8\u9001<\/td>\n<td>\u7528\u6237 + Bot<\/td>\n<\/tr>\n<tr>\n<td><strong>Bot \u670d\u52a1\u5668<\/strong><\/td>\n<td>\u5904\u7406\u903b\u8f91\u3001\u8c03\u7528 API<\/td>\n<td>\u4ec5 Telegram<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<blockquote>\n<p><strong>\u6700\u7ec8\u7b54\u6848<\/strong>\uff1a<br \/>\n<strong>Bot \u670d\u52a1\u5668\u4e0e\u7528\u6237\u4e4b\u95f4\u6ca1\u6709\u76f4\u63a5\u901a\u4fe1\uff0c\u6240\u6709\u4ea4\u4e92 100% \u901a\u8fc7 Telegram \u5b98\u65b9\u670d\u52a1\u5668\u4e2d\u8f6c\u3002<\/strong><\/p>\n<\/blockquote>\n<hr \/>\n<p><strong>\u63a8\u8350\u751f\u4ea7\u65b9\u6848<\/strong>\uff1a<br \/>\n<code>Webhook + HTTPS + Secret Token + \u516c\u7f51\u670d\u52a1\u5668<\/code><\/p>\n<hr \/>","protected":false},"excerpt":{"rendered":"<p>\u672c\u7bc7\u6240\u6709\u5185\u5bb9\u7686\u4e3agrok\u56de\u590d \u6838\u5fc3\u7ed3\u8bba\uff1a Bot \u670d\u52a1\u5668\u4e0e\u7528\u6237\u4e4b\u95f4\u65e0\u76f4\u63a5\u901a\u4fe1\uff0c\u6240\u6709\u4ea4\u4e92\u5fc5\u987b\u901a\u8fc7 Telegra &hellip; <a href=\"https:\/\/blog.poker-x-studio.com\/index.php\/2025\/11\/09\/telegram-bot-%e6%a8%a1%e5%9e%8b%e4%b8%8b%ef%bc%9abot-%e6%9c%8d%e5%8a%a1%e5%99%a8%e3%80%81telegram-%e6%9c%8d%e5%8a%a1%e5%99%a8%e4%b8%8e%e7%94%a8%e6%88%b7%e4%b9%8b%e9%97%b4%e7%9a%84%e4%ba%a4%e4%ba%92\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">Telegram Bot \u6a21\u578b\u4e0b\uff1aBot \u670d\u52a1\u5668\u3001Telegram \u670d\u52a1\u5668\u4e0e\u7528\u6237\u4e4b\u95f4\u7684\u4ea4\u4e92\u673a\u5236<\/span><\/a><\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[3],"tags":[],"class_list":["post-166","post","type-post","status-publish","format-standard","hentry","category-telegram"],"_links":{"self":[{"href":"https:\/\/blog.poker-x-studio.com\/index.php\/wp-json\/wp\/v2\/posts\/166","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.poker-x-studio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.poker-x-studio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.poker-x-studio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.poker-x-studio.com\/index.php\/wp-json\/wp\/v2\/comments?post=166"}],"version-history":[{"count":5,"href":"https:\/\/blog.poker-x-studio.com\/index.php\/wp-json\/wp\/v2\/posts\/166\/revisions"}],"predecessor-version":[{"id":171,"href":"https:\/\/blog.poker-x-studio.com\/index.php\/wp-json\/wp\/v2\/posts\/166\/revisions\/171"}],"wp:attachment":[{"href":"https:\/\/blog.poker-x-studio.com\/index.php\/wp-json\/wp\/v2\/media?parent=166"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.poker-x-studio.com\/index.php\/wp-json\/wp\/v2\/categories?post=166"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.poker-x-studio.com\/index.php\/wp-json\/wp\/v2\/tags?post=166"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}