วันอาทิตย์ที่ 14 มิถุนายน พ.ศ. 2558

[HOW TO] LINE Bot on Raspberry PI with LINE API Python

ไม่มีความคิดเห็น
[HOW TO] LINE Bot on Raspberry PI with LINE API Python   

1. ทำการอัพเดท packet ของ raspbian ก่อน
sudo apt-get install update
sudo apt-get install upgrade
 2. ซึ่ง [LINE API Python] จำเป็นต้องลง packet python 
sudo apt-get install python
sudo apt-get install python-pip
3. ทำการ download [LINE API Python] จาก https://github.com/carpedm20/LINE ในนี้จะมีทั้งวิธีใช้ด้วย
wget https://github.com/carpedm20/LINE/archive/master.zip
unzip master.zip
cd LINE-master/
4. ทำการแก้ไข ไฟล์ api.py ให้สามารถเชื่อมต่อกับ server หลักได้
ทำการเพิ่ม สคริป ภายใต้
def ready(self):
  """
  After login, make `client` and `client_in` instance
  to communicate with LINE server
  """
เอาเครื่องหมาย + ออกด้วย
+        self.transport = THttpClient.THttpClient(self.LINE_HTTP_URL)
 +        self.transport_in = THttpClient.THttpClient(self.LINE_HTTP_IN_URL)
 +        self.transport.setCustomHeaders(self._headers)
 +        self.transport_in.setCustomHeaders(self._headers)
 +        self.protocol = TCompactProtocol.TCompactProtocol(self.transport)
 +        self.protocol_in = TCompactProtocol.TCompactProtocol(self.transport_in)
 +        self._client = CurveThrift.Client(self.protocol)
 +        self._client_in = CurveThrift.Client(self.protocol_in)
 +        self.transport.open()
 +        self.transport_in.open()

5. ทำการรันไฟล์ config.py และ ติดตั้งด้วย setup.py
python config.py
python setup.py install

6. หากไม่มี Error  ใดๆในการติดตั้ง ให้ทดลองการส่งข้อความด้วยไฟล์ echobot.py ใน examples (อย่าลืมเข้าไปแก้ไฟล์ โดยการใส่ ID และ Pass ด้วยนะครับ)
vi echobot.py



เพิ่มเติม script checks status
vi check.py
import subprocess
from line import LineClient, LineGroup, LineContact

try:
    client = LineClient("ID", "PASS")
    #client = LineClient(authToken="TOKEN")
except:
    print "Login Failed"

while True:
    op_list = []

    for op in client.longPoll():
        op_list.append(op)

    for op in op_list:
        sender   = op[0]
        receiver = op[1]
        message  = op[2]

        msg = message.text
        if 'GetRAM' in msg:
                proc=subprocess.Popen('egrep --color "Mem|Cache|Swap" /proc/meminfo', shell=True, stdout=subprocess.PIPE, )
                output=proc.communicate()[0]
                sender.sendMessage("[%s] %s" % (sender.name, output))

        if 'GetDATE' in msg:
                proc=subprocess.Popen('date', shell=True, stdout=subprocess.PIPE, )
                output=proc.communicate()[0]
                sender.sendMessage("[%s] %s" % (sender.name, output))

        if 'GetKERNAL' in msg:
                proc=subprocess.Popen('uname -a', shell=True, stdout=subprocess.PIPE, )
                output=proc.communicate()[0]
                sender.sendMessage("[%s] %s" % (sender.name, output))


ขอบคุณ Api ดีๆจาก https://github.com/carpedm20/LINE
เครดิต : ตาเล็ก วินโด้

ไม่มีความคิดเห็น :

แสดงความคิดเห็น