make telegram bot

telegram bot만들기
python telegram으로 챗봇 만들기

1. 환경준비
    Centos 7.x
    python-telegram-bot

2. install telegram-bot
(base) [min@zepp ~]$ pip install python-telegram-bot --upgrade
Requirement already up-to-date: python-telegram-bot in ./anaconda3/lib/python3.7/site-packages (11.1.0)
Requirement already satisfied, skipping upgrade: certifi in ./anaconda3/lib/python3.7/site-packages (from python-telegram-bot) (2019.6.16)
Requirement already satisfied, skipping upgrade: future>=0.16.0 in ./anaconda3/lib/python3.7/site-packages (from python-telegram-bot) (0.17.1)
Requirement already satisfied, skipping upgrade: cryptography in ./anaconda3/lib/python3.7/site-packages (from python-telegram-bot) (2.4.2)
Requirement already satisfied, skipping upgrade: cffi!=1.11.3,>=1.7 in ./anaconda3/lib/python3.7/site-packages (from cryptography->python-telegram-bot) (1.11.5)
Requirement already satisfied, skipping upgrade: six>=1.4.1 in ./anaconda3/lib/python3.7/site-packages (from cryptography->python-telegram-bot) (1.12.0)
Requirement already satisfied, skipping upgrade: asn1crypto>=0.21.0 in ./anaconda3/lib/python3.7/site-packages (from cryptography->python-telegram-bot) (0.24.0)
Requirement already satisfied, skipping upgrade: idna>=2.1 in ./anaconda3/lib/python3.7/site-packages (from cryptography->python-telegram-bot) (2.8)
Requirement already satisfied, skipping upgrade: pycparser in ./anaconda3/lib/python3.7/site-packages (from cffi!=1.11.3,>=1.7->cryptography->python-telegram-bot) (2.19)
(base) [min@zepp ~]$ 

3. create chatbot id
## 먼저 telegram 챗팅창에서 @BotFather를 추가하고
## @BogFather에게 /start 
## chatbot 을 생성합니다


 ## 생성된 token을 python code에서 사용합니다
 ## chatbot 에게 아이콘을 만들어 줄께요.


## test source
import logging
import telegram
from telegram.error import NetworkError, Unauthorized
from time import sleep

update_id = None

def main():
    global update_id
    bot = telegram.Bot('000000000:XXXXXXXXXXXXXXXXXXXXXXX')

    try:
        update_id = bot.get_updates()[0].update_id
    except IndexError:
        update_id = None

    while True:
        try:
            echo(bot)
        except NetworkError:
            sleep(1)
        except Unauthorized:
            update_id += 1


def echo(bot):
    global update_id
    for update in bot.get_updates(offset=update_id, timeout=10):

        update_id = update.update_id + 1
        print(update_id)
        if update.message: 
            print(update.message.text)
            if update.message.text is not None:
                update.message.reply_text(update.message.text)

if __name__ == '__main__':
    main()

## result


댓글

이 블로그의 인기 게시물

[!] CDN: trunk URL couldn't be downloaded: https://cdn.cocoapods.org/CocoaPods-version.yml Response: URL using bad/illegal format or missing URL

starcraft map

Data Analysis with Superset - boardless chart