Python3 allows the public account to regularly send daily reports and images on a daily basis,

Source: Internet
Author: User

Python3 allows the public account to regularly send daily reports and images on a daily basis,

The example in this article shares with you the code for daily scheduled sending of public accounts in python3 for your reference. The specific content is as follows:

The procedure is as follows: first apply for a public number and find the interface file. After reading this, we found that the logon token is obtained through the corpid (Enterprise key) and corpsecret (application key), and each url is operated through this token.

I am using an enterprise, so I have a department. Other public accounts are similar. The result is as follows:

#-*-Coding: UTF-8-*-import requestsimport jsonimport timeurl0 = 'https: // your get_group_id (): # view the Department and Member values_address = {'corpid ': 'Your corpid ', 'cortana credt': Address Book Cortana credt',} req = requests. post (url0, params = values_address) data = json. loads (req. text) token = data ["access_token"] url_department = "https://qyapi.weixin.qq.com/cgi-bin/department/list? Access_token = "+ token # Department r_department = requests. get (url_department) result_department = r_department.json () result_department_no = result_department ['department '] print ("*** the obtained department information is as follows:") for item in result_department_no: print ("[Department]:", item ['id'], "[Department name]:", item ['name'], "[parent department]:", item ['parentid'], "[sequence number]:", item ['order']) print ("*** the obtained member information is as follows :") for I in range (len (result_department_no): I = I + 1 url_member = "Https://qyapi.weixin.qq.com/cgi-bin/user/simplelist? Access_token = % s & department_id = % s & fetch_child = FETCH_CHILD "% (token, I) # member r_member = requests. get (url_member) result_member = r_member.json () result_member_no = result_member ['userlist'] for item in result_member_no: print ("[member id]:", item ['userid'], "[member name]:", item ['name'], "[department]:", item ['department ']) return result_department_no, result_member_nodef upload_img (): values_address = {'corpid ':' Your Cortana cret ', 'cortana cret': 'app Cortana cret',} req = requests. post (url0, params = values_address) data = json. loads (req. text) token = data ["access_token"] print ("*** the token has been obtained. ") url_upimg =" https://qyapi.weixin.qq.com/cgi-bin/media/upload? Access_token = % s & type = % s "% (token," image ") files = {'filename': ('xn. JPG ', open ("D: \ DOCS \ DAY \ email/DRjpg. JPG ", 'rb')} # explicitly set the file name values_upimg = {" Content-Type ": 'multipart/form-data; boundary =" ---- WebKitFormBoundaryn5UouHKhfu8g2XNp ";', "Content-Length": '2014; boundary = "---- WebKitFormBoundaryn5UouHKhfu8g2XNp"; ', "Content-Disposition": 'form-data; name = "image"; boundary = ---- WebKitFormBoundaryn5Uo UHKhfu8g2XNp; ', "content-type": "application/octet-stream; boundary = ---- WebKitFormBoundaryn5UouHKhfu8g2XNp;"} req_upimg = requests. post (url_upimg, files = files, data = values_upimg) data = json. loads (req_upimg.text) media_id = data ['Media _ id'] print ("*** you have obtained the id required for the clip. ") return token, media_iddef send_msg (token, media_id): # Send image url =" https://qyapi.weixin.qq.com/cgi-bin/message/send? Access_token = "+ token values = {" touser ":" @ all "," toparty ":" 2 ", # ********************************** "msgtype ": "image", "agentid": 1000003, "image": {"media_id": media_id}, "safe": 0} data = json. dumps (values) req = requests. post (url, data) print ("returned result:", req. text) return req # print the returned message while True: current_time = time. localtime (time. time () if (current_time.tm_hour = 8) and (current_time.tm_min = 13) and (current_time.tm_sec = 50): (bytes, result_member_no) = get_group_id, media_id) = upload_img () send_msg (token, media_id) time. sleep (1)

The result is as follows:

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.