OrangePi 를 Telegram Bot 으로 활용하는 방법~


Telegram 은 카카오톡과 같은 메신저라고 생각하면 되는데,

API 가 잘 Open 되어 있어 다양하게 활용되고 있다.


Telegram Bot 은 사람이 아닌 Telegram User 로 생각하면 되는데, 

서버에서 구동하는 Code 로 사람인 User들과 interface 를 담당한다.


아래는 Python 기반의 telepot 을 사용해 telegram bot 을 만드는 방법이다.

우선 python을 설치 해 주고, package install 을 위해 pip 도 설치해 준다.

$ sudo apt-get install python python-pip python-dev build-essential

repository 에서 pip를 찾을 수 없는 경우 아래와 같이 설치 하면 된다.

$ sudo apt-get install python-setuptools

$ sudo easy_install pip

pip 로 telepot 을 설치 한다.

$ sudo pip install telepot

$ sudo pip install telepot --upgrade


telepot 을 설치하면 telepot 을 import 해 python 코드를 만들면 된다.

이때, telegram bot 에 할당되는 Token 이 필요한데, token 은 BotFather 에게 발급 받으면 된다.

telegram 메신저로 @BotFather 에게 /newbot 이라 메시지를 보내면 BotFather가 token을 발급해 준다.


아래는 telepot 을 활용한 기본적인 Bot code이다. (메시지를 보내면 같은 메아리를 보내준다.)

Bot.py

import sys

import time

import telepot


def handle(msg):

content_type, chat_type, chat_id = telepot.glance(msg)

print(content_type, chat_type, chat_id)


if content_type == 'text':

bot.sendMessage(chat_id, msg['text'])


TOKEN = 'INPUT YOUR OWN TOKEN HERE'


bot = telepot.Bot(TOKEN)

bot.message_loop(handle)

print('Listening ...')


while 1:

time.sleep(10)

* https://recall2300.github.io/2017-01-01/telegram-bot/ 을 참고하였슴다

아래와 같이 python 을 실행하여 Listening ... 이 출력되고, telegram 으로 나의 Bot에게 메시지를 보냈을 때 메아리가 들려오면 이상 무!

$ python bot.py


'Programming > OrangePi' 카테고리의 다른 글

sftp 네트워크 드라이브 연결  (0) 2018.04.05
OrangePi Plus 2 + SATA  (0) 2017.10.02
OrangePi Fan on GPIO  (0) 2017.09.15
U+ 공유기 포트포워딩 문제  (0) 2017.09.09
OrangePi Plus 2 Ubuntu 설치  (0) 2017.06.17

+ Recent posts