python send line notify

Solutions on MaxInterview for python send line notify by the best coders in the world

showing results for - "python send line notify"
Nicolò
13 Apr 2018
1"""line_notify.py
2  
3For sending a LINE Notify message (with or without image)
4  
5Reference: https://engineering.linecorp.com/en/blog/using-line-notify-to-send-messages-to-line-from-the-command-line/
6"""
7  
8import requests
9  
10  
11URL = 'https://notify-api.line.me/api/notify'
12  
13  
14def send_message(token, msg, img=None):
15    """Send a LINE Notify message (with or without an image)."""
16    headers = {'Authorization': 'Bearer ' + token}
17    payload = {'message': msg}
18    files = {'imageFile': open(img, 'rb')} if img else None
19    r = requests.post(URL, headers=headers, params=payload, files=files)
20    if files:
21        files['imageFile'].close()
22    return r.status_code
23  
24  
25def main():
26    import os
27    import sys
28    import argparse
29    try:
30        token = os.environ['LINE_TOKEN']
31    except KeyError:
32        sys.exit('LINE_TOKEN is not defined!')
33    parser = argparse.ArgumentParser(
34        description='Send a LINE Notify message, possibly with an image.')
35    parser.add_argument('--img_file', help='the image file to be sent')
36    parser.add_argument('message')
37    args = parser.parse_args()
38    status_code = send_message(token, args.message, args.img_file)
39    print('status_code = {}'.format(status_code))
40  
41  
42if __name__ == '__main__':
43    main()
44
similar questions
queries leading to this page
python send line notify