1. 程式人生 > >Python *args **kargs 傳參 釘釘自定義引數傳送訊息

Python *args **kargs 傳參 釘釘自定義引數傳送訊息

Python *args  **kargs  傳參

Python *args

*args:(表示的就是將實參中按照位置傳值,多出來的值都給args,且以元組的方式呈現)

def foo(x,*args):
    print(x);
    print()
    print(args);

foo(1,2,3,4,5)

Python   **kargs

**kwargs:(表示的就是

def foo(c,b,d,a=1):
    print(a);
    print(b);
    print(c);
    print(d)
foo(**{'c':3,'b':2,'d':4})

形參中按照關鍵字傳值把多餘的傳值以字典的方式呈現)

釘釘訊息自動化


import requests;
import json


# 釘釘訊息傳送
def SendDingTalkMsg(content,token,atMobiles,isAtAll=False):

    url=f"https://oapi.dingtalk.com/robot/send?access_token={token}"

    HEADERS={"Content-Type":"application/json;charset=utf-8"}

    String_textMsg={"msgtype":"text",
                    "text":{
                            "content":content
                            },
                     "at": {
                 "atMobiles": atMobiles,
                 "isAtAll": isAtAll
                 }
                }

    String_textMsg=json.dumps(String_textMsg)

    res=requests.post(url,data=String_textMsg,headers=HEADERS)

    return(res.text)
    
content = 'test:憑一個男人的直覺,開心就好';
token = 'XXXXXX';
atMobiles = ['xxxx','xxxx']

l = [content,token,atMobiles]

res_text = SendDingTalkMsg(*l,isAtAll=True)
if eval(res_text)['errmsg'] == 'ok':
    print('釘釘訊息傳送成功');
else:
    print('釘釘訊息傳送失敗');