[텔레그램 API] 우분투에서 텔레그램 API를 이용한 챗봇 만들기
1. 컴파일이나, CLI를 구동하는데 필요한 패키지를 설치한다.
$ sudo apt-get install libreadline-dev libconfig-dev libssl-dev lua5.2 liblua5.2-dev libevent-dev make git-core zlib1g-dev
2. Telegram-cli 소스 다운받고 빌드하기.
$ git clone --recursive https://github.com/vysheng/tg.git && cd tg $ ./configure $ make
make error가 났다.. 아래처럼 에러가 떴는데...
"checking for zlib.h... no" or "no zlib found" error
tgl/queries.c: In function ‘_tgl_do_send_photo’: tgl/queries.c:2091:10: error: cast between incompatible function types from ‘void (*)(struct tgl_state *, void *, int, struct tgl_message *)’ to ‘void (*)(struct tgl_state *, void *, int)’ [-Werror=cast-function-type] ((void (*)(struct tgl_state *, void *, int))callback) (TLS, callback_extra, 0); ^ tgl/queries.c:2108:10: error: cast between incompatible function types from ‘void (*)(struct tgl_state *, void *, int, struct tgl_message *)’ to ‘void (*)(struct tgl_state *, void *, int)’ [-Werror=cast-function-type] ((void (*)(struct tgl_state *, void *, int))callback) (TLS, callback_extra, 0); ^ tgl/queries.c:2141:10: error: cast between incompatible function types from ‘void (*)(struct tgl_state *, void *, int, struct tgl_message *)’ to ‘void (*)(struct tgl_state *, void *, int)’ [-Werror=cast-function-type] ((void (*)(struct tgl_state *, void *, int))callback) (TLS, callback_extra, 0); ^ cc1: all warnings being treated as errors make: *** [Makefile.tgl:20: objs/queries.o] Error 1
보니까, apt-get으로 이것저것을 더 깔아야했다.
아래 명령 추가 입력 후, make clean 뒤에 다시 make한다.(아래처럼)
apt-get install libgcrypt20-dev libssl-dev
make clean
./configure --disable-openssl --prefix=/usr CFLAGS="$CFLAGS -w"
make
3. Telegram-cli 실행하기
$ bin/telegram-cli -k server.pub
apt-get install telegram-cli
아래처럼 핸드폰인증을 진행한다.
root@terryvery-14ZD950-GX70K:/tg/bin# telegram-cli -k server.pub
change_user_group: can't find the user telegramd to switch to
Telegram-cli version 1.4.1, Copyright (C) 2013-2015 Vitaly Valtman
Telegram-cli comes with ABSOLUTELY NO WARRANTY; for details type `show_license'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show_license' for details.
Telegram-cli uses libtgl version 2.1.0
I: config dir=[/root/.telegram-cli]
[/root/.telegram-cli] created
[/root/.telegram-cli/downloads] created
>
>
> change_user_group: can't find the user telegramd
FAIL: 38: can not find command 'change_user_group:'
> d
FAIL: 38: can not find command 'd'
> ls
FAIL: 38: can not find command 'ls'
> created
FAIL: 38: can not find command 'created'
phone number: +8210나의핸드폰번호뒤에
code ('CALL' for phone code): 나의핸드폰코드
User terryb online (was online [2021/01/04 18:39:59])
아랫분 대박이다..(챗봇 알람방법 나옴..)
m.blog.naver.com/PostView.nhn?blogId=snoopyjk&logNo=221271982391&proxyReferer=https:%2F%2Fwww.google.com%2F
4. chat bot 만들기
우선 텔레그램을 들어가서 아래 사진처럼 API_TOKEN을 받는다.
mkdir tg/bot
git clone https://github.com/eternnoir/pyTelegramBotAPI.git
sudo apt-get install python3 python3-setuptools python3-pip
pip3 install -U pip
python3 setup.py install
cd examples
vi echo_bot.py에 들어가서, API_TOKEN에 인증키를 넣어준다.
#!/usr/bin/python
# This is a simple echo bot using the decorator mechanism.
# It echoes any incoming text messages.
import telebot
API_TOKEN = '안알려주지롱'
bot = telebot.TeleBot(API_TOKEN)
terry_message1 = "Hello My name is Terry"
terry_message2 = "Hello My name is Terryㅋㅋㅋㅋㅋㅋ"
# Handle '/start'
@bot.message_handler(commands=['start'])
def send_welcome(message):
bot.reply_to(message, terry_message1)
# Handle '/help'
@bot.message_handler(commands=['help'])
def send_welcome(message):
bot.reply_to(message, terry_message2)
# Handle all other messages with content_type 'text' (content_types defaults to ['text'])
@bot.message_handler(func=lambda message: True)
def echo_message(message):
bot.reply_to(message, message.text)
bot.polling()
python3 echo_bot.py
로 구동.
5. chat bot 사용해보기
내가 만든 봇 검색
/start 입력
글 확인