{"id":27360,"date":"2017-08-24T09:15:55","date_gmt":"2017-08-24T01:15:55","guid":{"rendered":"https:\/\/tomchun.tw\/tomchun\/?p=27360"},"modified":"2017-08-24T09:50:03","modified_gmt":"2017-08-24T01:50:03","slug":"node-js-telegram-bot","status":"publish","type":"post","link":"https:\/\/tomchun.tw\/tomchun\/2017\/08\/24\/node-js-telegram-bot\/","title":{"rendered":"\u7528node.js\u4f86\u8a2d\u5b9atelegram bot \u6a5f\u5668\u4eba\u6559\u5b78\u3001\u81ea\u52d5\u56de\u8986"},"content":{"rendered":"<p style=\"text-align: center;\"><strong><span style=\"color: #0000ff;\">node.js telegram bot \u6a5f\u5668\u4eba\u6559\u5b78<br \/>\n<!--more-->telegram\u53d6\u5f97\u6a5f\u5668\u4ebaBOT<br \/>\n<\/span><span style=\"color: #0000ff;\">\u627e\u5230 BotFather<\/span><\/strong><br \/>\n<strong><span style=\"color: #0000ff;\">\u8f38\u5165\/START<\/span><span style=\"color: #0000ff;\"><br \/>\n<strong>\/newbot<\/strong><br \/>\n\u53d6\u540d\u5b57<br \/>\n\u8a2d\u5b9aID<br \/>\n\u53d6\u5f97\u81ea\u5df1\u5c08\u5c6c\u9023\u7d50<br \/>\n\u53d6\u5f97API KEY TOKEN<br \/>\n&#8212;&#8211;<br \/>\n\u5b89\u88ddnode-v6.11.2-x64<br \/>\n\u81ea\u5df1\u5efa\u7acb\u4e00\u500b\u8cc7\u6599\u593enodejsbot<\/span><\/strong><br \/>\n<strong><span style=\"color: #0000ff;\">\u7136\u5f8c\u5728\u547d\u4ee4\u884c\u4ecb\u9762\u4f7f\u7528cd\u6307\u4ee4\u9032\u5165\u8a72\u8cc7\u6599\u593e<\/span><\/strong><br \/>\n<strong><span style=\"color: #0000ff;\">cd nodejsbot<\/span><\/strong><br \/>\n<strong><span style=\"color: #0000ff;\">npm\u6307\u4ee4\u4f5c\u521d\u59cb\u5316<\/span><\/strong><br \/>\n<strong><span style=\"color: #0000ff;\">npm init<\/span><\/strong><br \/>\n<strong><span style=\"color: #0000ff;\">\u6703\u81ea\u52d5\u5efa\u7acb\u4e00\u500bpackage.json\u65b9\u4fbf\u7ba1\u7406npm\u7684library<\/span><\/strong><br \/>\n<strong><span style=\"color: #0000ff;\">\u5b89\u88dd<\/span><\/strong><br \/>\n<strong><span style=\"color: #0000ff;\">npm install &#8211;save node-telegram-bot-api <\/span><\/strong><br \/>\n<strong><span style=\"color: #0000ff;\">npm install &#8211;save urlencode<\/span><\/strong><br \/>\n<strong><span style=\"color: #0000ff;\">\u5efa\u7acb123.js\u7a0b\u5f0f\u78bc<\/span><\/strong><br \/>\n<strong><span style=\"color: #0000ff;\">cmd\u57f7\u884c123.js\u5373\u53ef<\/span><\/strong><br \/>\n<strong><span style=\"color: #0000ff;\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-15330 aligncenter\" src=\"https:\/\/tomchun.tw\/tomchun\/imguploads\/2017\/08\/88038.jpg\" alt=\"\" width=\"384\" height=\"682\" \/>\u5b98\u65b9\u6e2c\u8a66\u7a0b\u5f0f\u78bc<\/span><\/strong><br \/>\n<strong><span style=\"color: #0000ff;\">const TelegramBot = require(&#8216;node-telegram-bot-api&#8217;);<\/span><\/strong><br \/>\n<strong><span style=\"color: #0000ff;\">\/\/ replace the value below with the Telegram token you receive from @BotFather <\/span><\/strong><br \/>\n<strong><span style=\"color: #0000ff;\">const token = &#8216;<span style=\"color: #ff0000;\">YOUR_TELEGRAM_BOT_TOKEN<\/span>&#8216;;<\/span><\/strong><br \/>\n<strong><span style=\"color: #0000ff;\">\/\/ Create a bot that uses &#8216;polling&#8217; to fetch new updates <\/span><\/strong><br \/>\n<strong><span style=\"color: #0000ff;\">const bot = new TelegramBot(token, {polling: true});<\/span><\/strong><br \/>\n<strong><span style=\"color: #0000ff;\">\/\/ Matches &#8220;\/echo [whatever]&#8221; <\/span><\/strong><br \/>\n<strong><span style=\"color: #0000ff;\">bot.onText(\/\\\/echo (.+)\/, (msg, match) =&gt; {<\/span><\/strong><br \/>\n<strong><span style=\"color: #0000ff;\"> \/\/ &#8216;msg&#8217; is the received Message from Telegram <\/span><\/strong><br \/>\n<strong><span style=\"color: #0000ff;\"> \/\/ &#8216;match&#8217; is the result of executing the regexp above on the text content <\/span><\/strong><br \/>\n<strong><span style=\"color: #0000ff;\"> \/\/ of the message <\/span><\/strong><br \/>\n<strong><span style=\"color: #0000ff;\"> const chatId = msg.chat.id;<\/span><\/strong><br \/>\n<strong><span style=\"color: #0000ff;\"> const resp = match[1]; \/\/ the captured &#8220;whatever&#8221; <\/span><\/strong><br \/>\n<strong><span style=\"color: #0000ff;\"> \/\/ send back the matched &#8220;whatever&#8221; to the chat <\/span><\/strong><br \/>\n<strong><span style=\"color: #0000ff;\"> bot.sendMessage(chatId, resp);<\/span><\/strong><br \/>\n<strong><span style=\"color: #0000ff;\">});<\/span><\/strong><br \/>\n<strong><span style=\"color: #0000ff;\">\/\/ Listen for any kind of message. There are different kinds of <\/span><\/strong><br \/>\n<strong><span style=\"color: #0000ff;\">\/\/ messages. <\/span><\/strong><br \/>\n<strong><span style=\"color: #0000ff;\">bot.on(&#8216;message&#8217;, (msg) =&gt; {<\/span><\/strong><br \/>\n<strong><span style=\"color: #0000ff;\"> const chatId = msg.chat.id;<\/span><\/strong><br \/>\n<strong><span style=\"color: #0000ff;\"> \/\/ send a message to the chat acknowledging receipt of their message <\/span><\/strong><br \/>\n<strong><span style=\"color: #0000ff;\"> bot.sendMessage(chatId, &#8216;Received your message&#8217;);<\/span><\/strong><br \/>\n<strong><span style=\"color: #0000ff;\">});<\/span><\/strong><\/p>\n<div class=\"8174c08f3e8258bc782beea63dd170ee\" data-index=\"2\" style=\"float: none; margin:0px 0 0px 0; text-align:center;\">\n<center>\r\n\r\n<!-- \u4ea4\u63db01 -->\r\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-1525485034639690\"\r\n     crossorigin=\"anonymous\"><\/script>\r\n\r\n<ins class=\"adsbygoogle\"\r\n     style=\"display:block\"\r\n     data-ad-client=\"ca-pub-1525485034639690\"\r\n     data-ad-slot=\"2417472866\"\r\n     data-ad-format=\"auto\"\r\n     data-full-width-responsive=\"true\"><\/ins>\r\n<script>\r\n     (adsbygoogle = window.adsbygoogle || []).push({});\r\n<\/script>\r\n\r\n<!-- Google tag (gtag.js) -->\r\n<script async src=\"https:\/\/www.googletagmanager.com\/gtag\/js?id=G-ZQM6RQ7BCG\"><\/script>\r\n<script>\r\n  window.dataLayer = window.dataLayer || [];\r\n  function gtag(){dataLayer.push(arguments);}\r\n  gtag('js', new Date());\r\n\r\n  gtag('config', 'G-ZQM6RQ7BCG');\r\n<\/script>\r\n\r\n<br>\r\n\r\n\r\n\r\n\r\n<!-- \u57df\u52d5\u5ee3\u544a -->\r\n\r\n<ins class=\"clickforceads\" style=\"display:inline-block;\" data-ad-zone=\"7246\"><\/ins>\r\n\r\n<script async type=\"text\/javascript\" src=\"\/\/cdn.holmesmind.com\/js\/init.js\"><\/script>\r\n\r\n\r\n\r\n\r\n<!-- \u57df\u52d5\u5ee3\u544a -->\r\n\r\n\r\n<\/center>\n<\/div>\n\n<div style=\"font-size: 0px; height: 0px; line-height: 0px; margin: 0; padding: 0; clear: both;\"><\/div>","protected":false},"excerpt":{"rendered":"<p>node.js telegram bot \u6a5f\u5668\u4eba\u6559\u5b78<\/p>\n","protected":false},"author":1,"featured_media":27361,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-27360","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-3c"],"modified_by":"tomchun","_links":{"self":[{"href":"https:\/\/tomchun.tw\/tomchun\/wp-json\/wp\/v2\/posts\/27360","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tomchun.tw\/tomchun\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tomchun.tw\/tomchun\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tomchun.tw\/tomchun\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tomchun.tw\/tomchun\/wp-json\/wp\/v2\/comments?post=27360"}],"version-history":[{"count":0,"href":"https:\/\/tomchun.tw\/tomchun\/wp-json\/wp\/v2\/posts\/27360\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/tomchun.tw\/tomchun\/wp-json\/wp\/v2\/media\/27361"}],"wp:attachment":[{"href":"https:\/\/tomchun.tw\/tomchun\/wp-json\/wp\/v2\/media?parent=27360"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tomchun.tw\/tomchun\/wp-json\/wp\/v2\/categories?post=27360"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tomchun.tw\/tomchun\/wp-json\/wp\/v2\/tags?post=27360"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}