안녕하세요. 루크입니다.
https://youtu.be/kWIQe5CPpxI

 

이번 시간에는 텔레그램 봇을 만들어서 업무는 물론 부업에서도 활용할 수 있는 나만의 개인 비서 어플을 만들어보겠습니다.

자세한 설명은 유튜브를 참고 부탁드리겠습니다 ㅎㅎ

 

- 텔레그램 설치

 

 

- 서버 만들기

https://www.pythonanywhere.com/

Host, run, and code Python in the cloud: PythonAnywhere

Batteries included With Python versions 2.7, 3.3, 3.4, 3.5 and 3.6, and all the goodies you normally find in a Python installation, PythonAnywhere is also preconfigured with loads of useful libraries, like NumPy, SciPy, Mechanize, BeautifulSoup, pycrypto,

www.pythonanywhere.com

 

- 훅 설정하기

api.telegram.org/bot토큰/setWebhook?url=서버이름.pythonanywhere.com/토큰

코드

# A very simple Flask Hello World app for you to get started with...

import pandas as pd

from flask import Flask, request
import json
import urllib.parse as parse
import urllib.request as req
app = Flask(__name__)

df = pd.read_excel('/home/아이디/mysite/엑셀명.xlsx', sheet_name='시트이름')





def baseRequest(command='getMe'):
    baseURL = 'https://api.telegram.org/bot토큰/'
    res = req.urlopen(baseURL+command)
    result = json.loads(res.read())['result']
    return result

def sendMessage(chat_id, text):
    query = parse.urlencode([
        ('chat_id', chat_id),
        ('text',text)
    ])
    print(query)
    baseRequest('sendMessage?'+query)

@app.route('/')
def hello_world():
    return 'Hello from Flask!'

@app.route('/토큰', methods=['POST','GET'])
def telegram():
    data = request.get_json()
    print(data)
    chat_id = data['message']['chat']['id']
    text = data['message']['text']

    try:
        df3 = df[df['칼럼이름1']== text]
        sendMessage(chat_id,str(df3['칼럼이름2'].values))
    except:
        sendMessage(chat_id,text)

    return json.dumps({'success':True})
  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기