하얀설표 블로그




텔레그램봇 만드는 방법




( 수정됨)


텔레그램봇 만들기

텔레그램봇을 만드는 방법은 간단하다. 텔레그램에서 BotFather에게 "/newbot" 명령을 메세지로 전달하면 된다.
봇 생성 과정은 다음과 같이 진행된다.

/newbot

BotFather
Alright, a new bot. How are we going to call it? Please choose a name for your bot.

{채팅방에 표시되는 채팅봇의 이름}

BotFather
Good. Now let's choose a username for your bot. It must end in `bot`. Like this, for example: TetrisBot or tetris_bot.

{봇 관리에 표시되는 채팅봇의 이름}

BotFather
Done! Congratulations on your new bot. You will find it at t.me/{봇 관리에 표시되는 채팅봇의 이름}. You can now add a description, about section and profile picture for your bot, see /help for a list of commands. By the way, when you've finished creating your cool bot, ping our Bot Support if you want a better username for it. Just make sure the bot is fully operational before you do this.

Use this token to access the HTTP API:
{봇 api 토큰}
Keep your token secure and store it safely, it can be used by anyone to control your bot.

For a description of the Bot API, see this page: https://core.telegram.org/bots/api

텔레그램봇 기초 조작 방법

바로 파이썬 모듈을 통해 봇을 조작할 수도 있지만, url만 열 수 있다면 웹브라우저만으로도 텔레그램봇을 조작하는 것이 가능하다.
봇 사용에 앞서 봇이 어떻게 작동하는지를 알기 위해 간단한 봇 조작방법을 알아볼 것이다.

텔레그램봇에게 전송된 메세지 확인하는 방법

텔레그램봇을 통해 할 수 있는 가장 기초적인 방법들을 알아볼 것이다.
그 방법들은 봇에게 온 메세지를 확인하는 방법과, 봇을 통해 사람에게 메세지를 보내는 방법이다.

가장 먼저 생성한 봇에게 아무 메세지나 보낸 다음, 다음 url을 웹브라우저로 접속해보자.

https://api.telegram.org/bot{봇 api 토큰}/getUpdates

그러면 다음과 같이 봇이 받은 메세지가 들어있는 리스트를 확인할 수 있을 것이다.

{"ok":true,"result":[
  {"update_id":437713457,
    "message":
      {"message_id":2,"from":{"id":{메세지를 보낸 사람의 chat_id},"is_bot":false,"first_name":"{메세지를 보낸 사람의 이름}","language_code":"ko"},"chat":{"id":{메세지를 보낸 사람의 chat_id},"first_name":"Din{메세지를 보낸 사람의 이름},"type":"private"},"date":1691565967,"text":"123"}},
  {"update_id":437713458,
    "message":
      {"message_id":3,"from":{"id":{메세지를 보낸 사람의 chat_id},"is_bot":false,"first_name":"{메세지를 보낸 사람의 이름}","language_code":"ko"},"chat":{"id":{메세지를 보낸 사람의 chat_id},"first_name":"{메세지를 보낸 사람의 이름}","type":"private"},"date":1691565997,"text":"message to bot"}}
]}

봇을 생성한 직후에 봇에게 메세지를 보낸 사람은 자기 자신밖에 없을 것이기에 "{메세지를 보낸 사람의 chat_id} = 자기자신의 채팅 id"라고 할 수 있다.

텔레그램봇으로 메세지를 전송하는 방법

텔레그램봇으로 메세지를 전달하기 위해서는 3가지가 필요하다. 봇의 api 토큰, 보낼 메세지, 메세지를 받을 사람의 chat_id다.
자기자신의 chat_id를 방금 알아냈으니 이번에는 봇을 통해 나 자신에게 메세지를 보내볼 것이다.

다음 url에서 3가지 요소들을 채운 url을 웹브라우저로 열어보자.

https://api.telegram.org/bot{봇 api 토큰}/sendMessage?chat_id={메세지를 받을 사람의 chat_id}&text={보낼 메세지}

url이 제대로 작성되었다면 다음과 같은 페이지가 표시되고, 텔레그램봇이 보낸 메세지를 텔레그램에서 확인할 수 있다.

{"ok":true,
"result":
{
  "message_id":2,
  "from":{"id":{봇의 chat_id},"is_bot":true,"first_name":"{봇의 이름}","username":"{봇 관리에 표시되는 봇의 이름}"},
  "chat":{"id":{메세지를 받은 사람의 chat_id},"first_name":"{메세지를 받은 사람의 이름}","type":"private"},
  "date":1691629672,"text":"{보낸 메세지}"
}}

그 밖에 텔레그램봇으로 가능한 명령들

지금까지는 웹브라우저만으로도 가능한 텔레그램봇 조작 방법을 알아보았다.
이를 활용해 파이썬의 requests 모듈만으로도 충분히 봇을 조작하는 것도 가능하지만, 이미 만들어진 텔레그램봇 조작 모듈을 사용하는 것도 가능하다.
텔레그램봇 모듈을 사용한다면 스스로 코드를 작성하는 수고를 줄이고 텔레그램에서 제공하는 더 많은 기능들을 쉽게 사용할 수 있기 때문이다.

대표적으로 telepot과 python-telegram-bot 모듈이 있는데, 개인적으로 telepot 모듈을 사용하는 것을 선호한다.

텔레그램봇으로 가능한 명령들은 다음과 같다.
수행 가능한 명령의 수는 굉장히 많으며, 각 명령들의 설명은 텔레그램에서 제공하는 api 페이지에서 확인할 수 있다.

Available methods

  • getMe
  • logOut
  • close
  • sendMessage
  • Formatting options
  • forwardMessage
  • copyMessage
  • sendPhoto
  • sendAudio
  • sendDocument
  • sendVideo
  • sendAnimation
  • sendVoice
  • sendVideoNote
  • sendMediaGroup
  • sendLocation
  • sendVenue
  • sendContact
  • sendPoll
  • sendDice
  • sendChatAction
  • getUserProfilePhotos
  • getFile
  • banChatMember
  • unbanChatMember
  • restrictChatMember
  • promoteChatMember
  • setChatAdministratorCustomTitle
  • banChatSenderChat
  • unbanChatSenderChat
  • setChatPermissions
  • exportChatInviteLink
  • createChatInviteLink
  • editChatInviteLink
  • revokeChatInviteLink
  • approveChatJoinRequest
  • declineChatJoinRequest
  • setChatPhoto
  • deleteChatPhoto
  • setChatTitle
  • setChatDescription
  • pinChatMessage
  • unpinChatMessage
  • unpinAllChatMessages
  • leaveChat
  • getChat
  • getChatAdministrators
  • getChatMemberCount
  • getChatMember
  • setChatStickerSet
  • deleteChatStickerSet
  • getForumTopicIconStickers
  • createForumTopic
  • editForumTopic
  • closeForumTopic
  • reopenForumTopic
  • deleteForumTopic
  • unpinAllForumTopicMessages
  • editGeneralForumTopic
  • closeGeneralForumTopic
  • reopenGeneralForumTopic
  • hideGeneralForumTopic
  • unhideGeneralForumTopic
  • answerCallbackQuery
  • setMyCommands
  • deleteMyCommands
  • getMyCommands
  • setMyName
  • getMyName
  • setMyDescription
  • getMyDescription
  • setMyShortDescription
  • getMyShortDescription
  • setChatMenuButton
  • getChatMenuButton
  • setMyDefaultAdministratorRights
  • getMyDefaultAdministratorRights
  • Inline mode methods

봇 토큰을 잃어버렸을 때 다시 찾는 방법

봇 파더에게 "/mybots" 명령을 통해 자신이 생성한 봇의 토큰을 요청할 수 있다.

/mybots

BotFather
Choose a bot from the list below:
(자신이 생성한 봇들의 리스트가 버튼으로 표시된다.)

BotFather
Here it is: {채팅방에 표시되는 채팅봇의 이름} @{봇 관리에 표시되는 채팅봇의 이름}.
What do you want to do with the bot?
(봇 설정들이 버튼으로 표시된다.)
API Token을 선택한다.

BotFather
Here is the token for bot {채팅방에 표시되는 채팅봇의 이름} @{봇 관리에 표시되는 채팅봇의 이름}:
{봇 api 토큰}


공감 : 0







white.seolpyo.com