Python으로 Telegram 챗봇 시작하기

Minkang Heo
Jul 21, 2017 · 3 min read

다양한 방법으로 챗봇 만들기 자료들이 제공되고 있지만, Python을 이용해 직접 코딩하고 바로 서비스까지 가능하도록 BotHub.Studio 를 이용해 빠르게 챗봇을 제작하는 방법을 안내해 드립니다.

Telegram Bot 생성

텔레그램 검색창에서 @BotFather 를 찾습니다. 또는 t.me/BotFather 으로 연결 하셔도 됩니다.

I’m the BotFather

/newbot 명령을 이용해 새로운 봇을 생성합니다.

봇의 username은 중복될 수 없습니다. 창의적이고 유니크한 이름을 입력해 주세요 :)

챗봇 연동을 위해 Access Token을 기록해 두시면 됩니다.

챗봇 연결

챗봇 호스팅 서비스 BotHub.Studio 회원 가입 후 아래와 같이 CLI 도구를 설치합니다.

$ pip install bothub-cli

계정 연결을 위해 configure 을 실행합니다.

$ bothub configure

프로젝트 폴더를 생성합니다.

$ mkdir MyBot
$ cd MyBot
$ bothub init

init 작업이 완료되면 기본 코드가 생성됩니다.

|-- bothub
| |-- bot.py
| `-- __init__.py
|-- bothub.yml
|-- requirements.txt
`-- tests

기본 코드인 bot.py 에는 유저의 메시지를 그대로 따라하는 EchoBot 코드가 들어 있습니다.

그럼 실제 실행을 위해 위에서 생성한 텔레그램 Access Token을 연결합니다.

$ bothub channel add telegram --api-key=<Access Token>

API KEY에 Access Token을 입력하실땐 화살괄호 <>를 포함하지 않고 넣으셔야 합니다.

이제 서버로 프로젝트를 업로드 합니다.

$ bothub deploy

이제 텔레그램에서 챗봇이 잘 동작하는지 확인해 보면 됩니다.

자신의 챗봇은 @username 으로 검색하거나 t.me/<username> 을 통해서 접속할 수 있습니다.

본 아티클 관련 문의는 BotHub.Studio Community를 통해 지원받으실 수 있습니다. 챗봇 도입 및 제휴 문의는 bothub@bothub.sutdio로 연락해주세요.

BotHub.Studio (ko)

News, tutorials about BotHub.Studio chatbot hosting service

)

Minkang Heo

Written by

BotHub.Studio (ko)

News, tutorials about BotHub.Studio chatbot hosting service

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade