Skip to content

Commit

Permalink
1. fix list agent uri
Browse files Browse the repository at this point in the history
2. adjust python3
  • Loading branch information
yorks committed Jun 9, 2021
1 parent c1713a2 commit 4542883
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 29 deletions.
4 changes: 2 additions & 2 deletions corpwechat/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python2
#!/usr/bin/env python
#-*- coding: utf-8 -*-

__author__ = 'yorks.yang@163.com'
__version__ = '0.0.1'
__version__ = '0.0.2'

from .base import API
74 changes: 47 additions & 27 deletions corpwechat/base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python
#-*- coding: utf-8 -*-

import requests
Expand Down Expand Up @@ -47,8 +47,8 @@ def get_config(self):
content = fp.read()
fp.close()
return json.loads( content )
except Exception, e:
print e
except Exception as e:
print (e)
return {}

def save_config(self):
Expand All @@ -57,8 +57,8 @@ def save_config(self):
fw.write(json.dumps(self.config))
fw.close()
return True
except Exception, e:
print e
except Exception as e:
print (e)
return False

def get_token(self):
Expand All @@ -67,19 +67,19 @@ def get_token(self):
return self.config['access_tk']

if not self.config['corpid']:
print "missing corpid"
print ("missing corpid")
return False
if not self.config['corpsecret']:
print "missing corpsecret"
print ("missing corpsecret")
return False
print "get new token..."
#print "get new token..."
payload = {'corpid':self.config['corpid'], 'corpsecret':self.config['corpsecret']}
url = self.api_pre + 'gettoken'
r = requests.get(url, params=payload)
try:
ret = r.json()
except Exception, e:
print e
except Exception as e:
print (e)
return False
self.config['access_tk'] = ret['access_token']
self.config['tk_expires'] = int(time.time()) + ret['expires_in'] - 10
Expand Down Expand Up @@ -110,8 +110,8 @@ def _push_message(self, agentid=0, msgtype='text', content={}, touser='@all',
r = requests.post(url, json=payload, params={'access_token':tk})
try:
ret = r.json()
except Exception, e:
print e
except Exception as e:
print (e)
return False
return ret

Expand Down Expand Up @@ -225,14 +225,14 @@ def _upload(self, filepath, type_='file'):

fsize = os.path.getsize(filepath)
if type_ in ['image', 'voice'] and fsize >= 2*1024*1024:
print "file size too large > 2M"
print ("file size too large > 2M")
return False
if type_ == 'video' and fsize >= 10*1024*1024:
print "file size too large > 10M"
print ("file size too large > 10M")
return False

if type_ == 'file' and fsize >= 20*1024*1024:
print "file size too large > 20M"
print ("file size too large > 20M")
return False


Expand All @@ -242,8 +242,8 @@ def _upload(self, filepath, type_='file'):
r = requests.post(url, files=files, params=payload)
try:
ret = r.json()
except Exception, e:
print e
except Exception as e:
print (e)
return False
return ret

Expand All @@ -257,13 +257,13 @@ def upload_file(self, filepath):
return self._upload(filepath, 'file')

def get_agent_list(self):
url = self.api_pre + '/agent/list'
url = self.api_pre + 'agent/list'
payload = {'access_token':self.get_token() }
r = requests.get(url, params=payload)
try:
ret = r.json()
except Exception, e:
print e
except Exception as e:
print (e)
return False
return ret

Expand All @@ -272,14 +272,34 @@ def get_agent_list(self):


if __name__ == "__main__":

corpwx = API('config.json')
aid = None
content = None
import sys
try:
cf = sys.argv[1]
if not os.path.isfile(cf):
content = cf
cf = './config.json'
except:
cf = './config.json'

corpwx = API(cf)
tk = corpwx.get_token()
if not tk:
sys.exit(1)

ret = corpwx.get_agent_list()
for a in ret['agentlist']:
print "agentid:", a['agentid'], "name:", a['name']
aid = int(raw_input('please input which agentid to send msg:'))
content=raw_input('please input the msg to send to @all:')
print corpwx.push_text_msg(agentid=aid, content=content)
if len(ret['agentlist']) == 1:
aid = ret['agentlist'][0]['agentid']

try:
input = raw_input
except:
pass
if not aid:
for a in ret['agentlist']:
print ("agentid:", a['agentid'], "name:", a['name'])
aid = int(input('please input which agentid to send msg:'))
if not content:
content=input('please input the msg to send to @all:')
print (corpwx.push_text_msg(agentid=aid, content=content))

0 comments on commit 4542883

Please sign in to comment.