{"id":20501107,"url":"https://github.com/hosein2398/node-telegram-bot-api-tutorial","last_synced_at":"2026-01-28T15:30:49.730Z","repository":{"id":44007712,"uuid":"86852567","full_name":"hosein2398/node-telegram-bot-api-tutorial","owner":"hosein2398","description":"node-telegram-bot-api tutorial","archived":false,"fork":false,"pushed_at":"2022-02-25T19:44:18.000Z","size":784,"stargazers_count":351,"open_issues_count":11,"forks_count":48,"subscribers_count":11,"default_branch":"master","last_synced_at":"2025-03-05T19:51:05.772Z","etag":null,"topics":["messenger","telegram","tutorial"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hosein2398.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-03-31T19:16:10.000Z","updated_at":"2025-02-18T16:55:56.000Z","dependencies_parsed_at":"2022-07-09T14:46:22.194Z","dependency_job_id":null,"html_url":"https://github.com/hosein2398/node-telegram-bot-api-tutorial","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/hosein2398/node-telegram-bot-api-tutorial","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hosein2398%2Fnode-telegram-bot-api-tutorial","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hosein2398%2Fnode-telegram-bot-api-tutorial/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hosein2398%2Fnode-telegram-bot-api-tutorial/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hosein2398%2Fnode-telegram-bot-api-tutorial/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hosein2398","download_url":"https://codeload.github.com/hosein2398/node-telegram-bot-api-tutorial/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hosein2398%2Fnode-telegram-bot-api-tutorial/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28846337,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-28T15:15:36.453Z","status":"ssl_error","status_checked_at":"2026-01-28T15:15:13.020Z","response_time":57,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["messenger","telegram","tutorial"],"created_at":"2024-11-15T18:24:07.437Z","updated_at":"2026-01-28T15:30:49.714Z","avatar_url":"https://github.com/hosein2398.png","language":null,"readme":"# node-telegram-bot-api-tutorial\n\n\nThis is a beginners' guide for [node-telegram-bot-api](https://github.com/yagop/node-telegram-bot-api) .\n\n\n - [Creating new bot with BotFather](#Creating+new+bot+with+BotFather)\n - [First message](#First+message)\n - [Commands](#Commands)\n - [Keyboards](#Keyboards)\n - [User](#User)\n - [Inline keyboards](#Inline+Keybords)\n - [parse_mode](#parse_mode)\n - [Location and Number](#Location+and+Number)\n - [Interacting with groups and channels](#grpups+and+channel+interaction)\n\n \u003ca name=\"Creating+new+bot+with+BotFather\"\u003e\u003c/a\u003e\n### Creating new bot with BotFather\nThe following steps describe how to create a new bot:\n- Contact @BotFather in your Telegram messenger\n- To get a token, send BotFather a message that says \u003ccode\u003e/newbot\u003c/code\u003e\n- When asked for a name for your new bot choose something that ends with the word \u003ccode\u003ebot\u003c/code\u003e. For example, \u003ccode\u003emy_test_bot\u003c/code\u003e\n- If your chosen name is available, BotFather will send you a token\n- Save the token\n\n\u003cimg src=\"https://raw.githubusercontent.com/hosein2398/node-telegram-bot-api-tutorial/master/pics/BotFather.JPG\" height=\"500\" width=\"400\"\u003e\n\nOnce your bot is created, you can set a Description for it. `Description` is a message in middle of the page usually describing what the bot can do.\n\n\u003cimg src=\"https://raw.githubusercontent.com/hosein2398/node-telegram-bot-api-tutorial/master/pics/desc.JPG\" height=\"500\" width=\"500\"\u003e\n\nTo set Description for your bot in BotFather do the following:\n- Send \u003ccode\u003e/setdescription\u003c/code\u003e to BotFather\n- Select the bot for which you are writing a Description\n- Change the description and send it to BotFather\n\nThere are some other useful methods in BotFather which we won't cover in this tutorial like \u003ccode\u003e/setcommands\u003c/code\u003e and other.\n \u003ca name=\"First+message\"\u003e\u003c/a\u003e\n### First message\nOk now you're ready to go. Create a node project and install bot-api:\n\n    npm install --save node-telegram-bot-api\n\n   Create a file index.js (or any other name) and inside the file require node-telegram-bot-api:\n\n```js\nconst TelegramBot = require('node-telegram-bot-api');\n```\n   Then you need to assign your token which you got from BotFather:\n\n\n```js\nconst token = 'YOUR_TELEGRAM_BOT_TOKEN';\n```\n   And now create a new bot :\n\n```js\nconst bot = new TelegramBot(token, {polling: true});\n```\n  Let's try out our bot and do some real world things. We need to get messages that user sends us , to do so we would use following code:\n\n```js\nbot.on('message', (msg) =\u003e {\n\n     //anything\n\n});\n```\nLet's create simple greeting here. Here's big picture of our code :\n```js\nconst TelegramBot = require('node-telegram-bot-api');\nconst token = 'YOUR_TELEGRAM_BOT_TOKEN';\nconst bot = new TelegramBot(token, {polling: true});\n\nbot.on('message', (msg) =\u003e {\n\n  //anything\n\n});\n```\n  We were trying to greet and we'll do it here:\n\n```js\nbot.on('message', (msg) =\u003e {\n\nvar Hi = \"hi\";\nif (msg.text.toString().toLowerCase().indexOf(Hi) === 0) {\nbot.sendMessage(msg.chat.id,\"Hello dear user\");\n}\n\n});\n```\nOk , now open up your command prompt and type:\n\n    node index.js\n  Go to your bot and hit on \u003ccode\u003e/start\u003c/code\u003e and then type \u003ccode\u003e\"Hi\"\u003c/code\u003e to it:\n\n\u003cimg src=\"https://raw.githubusercontent.com/hosein2398/node-telegram-bot-api-tutorial/master/pics/first%20message.JPG\" height=\"500\" width=\"400\"\u003e\n\n  So now that you know how to send and receive messages in your bot you may want to put some salt on it:\n\n```js\nbot.on('message', (msg) =\u003e {\n\nvar hi = \"hi\";\nif (msg.text.toString().toLowerCase().indexOf(hi) === 0) {\nbot.sendMessage(msg.chat.id,\"Hello dear user\");\n}\n\nvar bye = \"bye\";\nif (msg.text.toString().toLowerCase().includes(bye)) {\nbot.sendMessage(msg.chat.id, \"Hope to see you around again , Bye\");\n}\n\n});\n```\nThis time we're using \"includes\" method so if user sends us anything containing \"bye\" word we'll send him back the message:\n\n\u003cimg src=\"https://raw.githubusercontent.com/hosein2398/node-telegram-bot-api-tutorial/master/pics/Bye.JPG\" height=\"500\" width=\"400\"\u003e\nAnd definitely you can use any other string method that you want.\n\n\n \u003ca name=\"Commands\"\u003e\u003c/a\u003e\n### Commands\nThat's really common to send user a message describing use of bot while he taps on \"/start\". (these are called [commands](https://core.telegram.org/bots#commands))\nTo do so :\n```js\nbot.onText(/\\/start/, (msg) =\u003e {\n\nbot.sendMessage(msg.chat.id, \"Welcome\");\n\n});\n```\n  Let's create another command that will send a picture to user:\n\n```js\nbot.onText(/\\/sendpic/, (msg) =\u003e {\n\nbot.sendPhoto(msg.chat.id,\"https://www.somesite.com/image.jpg\" );\n\n});\n```\nSo now if you write \"/sendpic\" on your bot an image will be sent.\nSending audios is same and simple you can use \"[sendAudio](https://github.com/yagop/node-telegram-bot-api/blob/master/doc/api.md#TelegramBot+sendAudio)\" method .\n\nNow you might have seen some pictures containing caption with them like the following picture.\n\n\u003cimg src=\"https://raw.githubusercontent.com/hosein2398/node-telegram-bot-api-tutorial/master/pics/CaptionJPG.JPG\" height=\"500\" width=\"400\"\u003e\n\n\nWell , How to to create these?\nAnswer is really simple you can send a caption with option on photo like so :\n```js\nbot.onText(/\\/sendpic/, (msg) =\u003e {\n\nbot.sendPhoto(msg.chat.id,\"https://www.somesite.com/image.jpg\",{caption : \"Here we go ! \\nThis is just a caption \"} );\n\n});\n```\n  So now you know how to create captions and how to go to new line in your messages by typing `\\n` .\n\n \u003ca name=\"Keyboards\"\u003e\u003c/a\u003e\n### Keyboards\nLet's go a step further and start working with [keyboards](https://core.telegram.org/bots#keyboards).\nkeyboards are actually the ones shown in this picture:\n\n\u003cimg src=\"https://raw.githubusercontent.com/hosein2398/node-telegram-bot-api-tutorial/master/pics/keyboard.jpg\" height=\"500\" width=\"400\"\u003e\n\n\nKeyboards are nothing but an easy way to send fixed messages. It's like you're not forcing users to write something down and send it to bot, but instead you're demonstrating them some options that they can tap on and send back as an answer.\nSo let's see how we can create Keyboards , we'll send a Keyboard on \"/start\" message:\n```js\nbot.onText(/\\/start/, (msg) =\u003e {\n\nbot.sendMessage(msg.chat.id, \"Welcome\", {\n\"reply_markup\": {\n    \"keyboard\": [[\"Sample text\", \"Second sample\"],   [\"Keyboard\"], [\"I'm robot\"]]\n    }\n});\n\n});\n```\nSo now if you run you will see:\n\n\u003cimg src=\"https://raw.githubusercontent.com/hosein2398/node-telegram-bot-api-tutorial/master/pics/Keyboards.JPG\" height=\"500\" width=\"400\"\u003e\n\nAs I said in fact Keyboards are not nothing but automatic type and send for user. There is no difference if you write \"I'm robot\" and sending on your own or you click on Keyboard. Let's do something simple when that \"I'm robot\"  is received.So add this up to your previous on message:\n```js\nbot.on('message', (msg) =\u003e {\nvar Hi = \"hi\";\nif (msg.text.toString().toLowerCase().indexOf(Hi) === 0) {\n    bot.sendMessage(msg.chat.id, \"Hello dear user\");\n}\nvar bye = \"bye\";\nif (msg.text.toString().toLowerCase().includes(bye)) {\n    bot.sendMessage(msg.chat.id, \"Hope to see you around again , Bye\");\n}\nvar robot = \"I'm robot\";\nif (msg.text.indexOf(robot) === 0) {\n    bot.sendMessage(msg.chat.id, \"Yes I'm robot but not in that way!\");\n}\n});\n```\n   So now if you go to your bot tap on start you see Keyboards and if you tap on I'm robot you'll see the message. Note that there is no difference if you type it or you send it by Keyboards.\n\n \u003ca name=\"User\"\u003e\u003c/a\u003e\n### User\nnode-telegram-bot-api does not have any method to get users information but in case if you want to, you can get information like so:\n```js\nvar Hi = \"hi\";\nif (msg.text.toString().toLowerCase().indexOf(Hi) === 0) {\n    bot.sendMessage(msg.from.id, \"Hello  \" + msg.from.first_name);\n}\n```\n\n\u003cimg src=\"https://raw.githubusercontent.com/hosein2398/node-telegram-bot-api-tutorial/master/pics/usersname.JPG\" height=\"500\" width=\"400\"\u003e\n\nAnd if you wanted to get user profile pictures you can use [getUserProfilePhotos](https://github.com/yagop/node-telegram-bot-api/blob/master/doc/api.md#telegrambotgetuserprofilephotosuserid-options--promise) .\n\n \u003ca name=\"Inline+Keybords\"\u003e\u003c/a\u003e\n### Inline Keybords\nThis section is under construction...\n\n\u003ca name=\"parse_mode\"\u003e\u003c/a\u003e\n### parse_mode\nIf you want to send messages with some style there, here is how it goes. \u003ccode\u003eparse_mode\u003c/code\u003e defines how you want you message to be rendered.You can define it inside in your options when sending message.\nAvailable option are HTML and Markdown.Let's see how it works in action:\n```js\nbot.on('message', (msg) =\u003e {\n\n var Hi = \"hi\";\n if (msg.text.toString().toLowerCase().indexOf(Hi) === 0) {\n   bot.sendMessage(msg.chat.id,\"\u003cb\u003ebold\u003c/b\u003e \\n \u003ci\u003eitalic\u003c/i\u003e \\n \u003cem\u003eitalic with em\u003c/em\u003e \\n \u003ca href=\\\"http://www.example.com/\\\"\u003einline URL\u003c/a\u003e \\n \u003ccode\u003einline fixed-width code\u003c/code\u003e \\n \u003cpre\u003epre-formatted fixed-width code block\u003c/pre\u003e\" ,{parse_mode : \"HTML\"});\n   }\n});\n```\nSo you get idea where \u003ccode\u003eparse_mode\u003c/code\u003e is defined.Now if we run this:\n\n\u003cimg src=\"https://raw.githubusercontent.com/hosein2398/node-telegram-bot-api-tutorial/master/pics/parse_mode.JPG\" height=\"500\" width=\"400\"\u003e\n\nSo that's how we do it with Html you can also write with Markdown.As mentioned in official api of Telegram only these tags are supported till now:\n\n```\n\u003cb\u003ebold\u003c/b\u003e, \u003cstrong\u003ebold\u003c/strong\u003e\n\u003ci\u003eitalic\u003c/i\u003e, \u003cem\u003eitalic\u003c/em\u003e\n\u003ca href=\"http://www.example.com/\"\u003einline URL\u003c/a\u003e\n\u003ccode\u003einline fixed-width code\u003c/code\u003e\n\u003cpre\u003epre-formatted fixed-width code block\u003c/pre\u003e\n```\nAnd for Markdown you can only use:\n```\n*bold text*\n_italic text_\n[text](http://www.example.com/)\n`inline fixed-width code`\n```\n \u003ca name=\"Location+and+Number\"\u003e\u003c/a\u003e\n### Location and Number\nThere are some methods that enable you to send users location.\nHere is an example:\n```js\nbot.on('message', (msg) =\u003e {\n    var location = \"location\";\n    if (msg.text.indexOf(location) === 0) {\n        bot.sendLocation(msg.chat.id,44.97108, -104.27719);\n        bot.sendMessage(msg.chat.id, \"Here is the point\");\n\n    }\n});\n\n```\n\n\u003cimg src=\"https://raw.githubusercontent.com/hosein2398/node-telegram-bot-api-tutorial/master/pics/Location.JPG\" height=\"500\" width=\"400\"\u003e\n\n\nAnd there is also [sendVenue](https://github.com/yagop/node-telegram-bot-api/blob/master/doc/api.md#telegrambotsendvenuechatid-latitude-longitude-title-address-options--promise).\nTo send a phone number you can use [sendContact](https://github.com/yagop/node-telegram-bot-api/blob/master/doc/api.md#TelegramBot+sendContact).\n\n \u003ca name=\"grpups+and+channel+interaction\"\u003e\u003c/a\u003e\n### Interacting with groups and channels\nOne interesting use of bots is in groups and channels.You can use bots to manage groups , receiving users messages and sending group messages. You can even send keyboards to members so that they can use the bot easier.\nFor better understanding of how bots can be useful in groups let's create one and add it to a group.\nSo we want to create a bot that says \"Have a nice day _Username_\" when any member of group said something containing \"Bye\" keyword.\nBefore doing anything you have to know that a bot has no accessibility to users messages unless we add it as of a administrator.\nAnd if don't do that bot will only has access to [Commands](https://github.com/hosein2398/node-telegram-bot-api-tutorial#commands) that any user sends to group.And all of this is because of the [privacy policy](https://core.telegram.org/bots#privacy-mode) that Telegram messenger follows which I think is right.\nSo let's get back to creating that bot , actually we don't have to do anything different than creating bot for a single user usage , everything is similar.\n```js\nbot.on('message', (msg) =\u003e {\n\nvar bye = \"bye\";\nif (msg.text.toString().toLowerCase().includes(bye)) {\nbot.sendMessage(msg.chat.id, \"Have a nice day \" + msg.from.first_name);\n}\n\n});\n```\n\nNow that we wrote codes go ahead and add this bot to a group , after adding it set it as of a administrator of the group so that it could get users messages.\nNow say something that includes \"bye\":\n\n\u003cimg src=\"https://raw.githubusercontent.com/hosein2398/node-telegram-bot-api-tutorial/master/pics/Bot.JPG\" height=\"500\" width=\"400\"\u003e\n\nNow let's do another thing , write some codes that bot will remove a person from group if they say a specific word.For instance you can create a bot that will remove member if they curse in group.\nHere we will write codes that if someone says anything containing \"idiot\" bot will remove that person from group.\n```js\nbot.on('message', (msg) =\u003e {\n\nvar what = \"idiot\";\nif (msg.text.includes(what)) {\nbot.kickChatMember(msg.chat.id,  msg.from.id);\n}\n\n});\n```\nHere we have used [kickChatMember](https://github.com/yagop/node-telegram-bot-api/blob/58566d2df5146611a6a3e6140ca616403d24a20e/README.md#telegrambotkickchatmemberchatid-userid--promise) method to remove a member it receives two parameters , first one is id of chat and second one is id of user which you want to remove.Now if you add this bot among admins of a group and somebody says something containing \"idiot\":\n\n\u003cimg src=\"https://raw.githubusercontent.com/hosein2398/node-telegram-bot-api-tutorial/master/pics/Bot_2.JPG\" height=\"400\" width=\"300\"\u003e\n\nWe can't cover all methods there are some other methods related to groups and channels: [getChat](https://github.com/yagop/node-telegram-bot-api/blob/master/doc/api.md#TelegramBot+getChat) , [getChatAdministrators](https://github.com/yagop/node-telegram-bot-api/blob/master/doc/api.md#TelegramBot+getChatAdministrators) , [getChatMembersCount](https://github.com/yagop/node-telegram-bot-api/blob/master/doc/api.md#telegrambotgetchatmemberscountchatid--promise) , [getChatMember](https://github.com/yagop/node-telegram-bot-api/blob/master/doc/api.md#telegrambotgetchatmemberchatid-userid--promise) , [leaveChat](https://github.com/yagop/node-telegram-bot-api/blob/master/doc/api.md#telegrambotleavechatchatid--promise)\n\n### Contributing\n+ If you found any typo or grammatically wrong sentence you can just make a PR.(In case you were not interested to, you can open an issue.)\n+ If you have anything in mind that can improve this tutorial please make a PR.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhosein2398%2Fnode-telegram-bot-api-tutorial","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhosein2398%2Fnode-telegram-bot-api-tutorial","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhosein2398%2Fnode-telegram-bot-api-tutorial/lists"}