---
# System prepended metadata

title: MicroPython傳送Line訊息測試

---

# MicroPython傳送Line訊息測試
```python=
import network, ubinascii
import time
import urequests as requests

wlan = network.WLAN(network.STA_IF)
ssid = 'Galaxy A71ED4A'#網路名稱
passwd = '00000000'#密碼

def main():
    active_wlan()
    if connect_sta():
        print(f'\nConnected ssid {ssid}. Network config: {wlan.ifconfig()}')
    else:
        print(f'\nFailed. Not Connected to: {ssid}')

def active_wlan():
    active = wlan.active()
    if not active:
        wlan.active(True)

def connect_sta():
    if wlan.isconnected():
        return True
    print(f'Trying to connect to {ssid}')
    wlan.connect(ssid, passwd)
    for retry in range(100):
        connected = wlan.isconnected()
        if connected:
            break
        time.sleep(0.1)
        print('.', end='')
    return connected

main()

def lineNotifyMessage(token, msg):
    headers = {"Authorization": "Bearer " + token}
    #串送的過程會以URL編碼因此空白會報錯，所以將空白改為%20(URL中的space)
    msg = msg.replace(' ', '%20')
    #這裡原本是用date傳的，但沒成功，所以改成了一般格式，後來與友人聊過後發現data傳送的為string，我推測應該是用於Restful風格的API，因此還需要修改成格式化，但我懶得改了，反正懂得都懂
    r = requests.post("https://notify-api.line.me/api/notify"+'?message='+msg, headers = headers)
    print(r)
message = 'Notify test!'
token = 'J7Pe04oytc0rOOZ4kos9TJ3YDvieGz19M466GKg0j4l'#使用著權杖
lineNotifyMessage(token, message)
```
