Q1)无法将松弛的用户名打印为“ @John”,而不是打印“ WLCLZ2LDP”
因此,对于变量“用户名”,它应打印松弛的用户名(例如:JohnDoe),但应将随机字母打印为WLCLZ2LDP
代码
@slack.RTMClient.run_on(event='message')
def say_hello(**payload):
try:
##0 get clients and payload
logging.info('msg received')
data = payload['data']
web_client = payload['web_client']
rtm_client = payload['rtm_client']
##0 - 1 Check if it is the first msg, not replied msg by me
# print(data)
if data.get('text') == None:
logging.info('This msg is my replied msg.')
return False
##0-2 Get channel info
channel_id = data['channel']
thread_ts = data['ts']
global user
user = data['user']
##1 get scenario submsg
msg = data['text']
retVal = analysis_msg(msg)
.
.
.
.code continuation
.
.
.
.mycursor = mydb.cursor()
for value in submsg:
print(value)
fulfilment=submsg[0]
sku=submsg[1]
environment=submsg[2]
payment=submsg[3]
ts = time.time()
date = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
orderNumber=res
username=user
print(fulfilment)
print(sku)
print(environment)
print(payment)
print(username)
print(orderNumber)
sqlformula = "INSERT INTO orderDetails (fulfilment,sku,environment,payment,orderNumber,date,user) VALUES (%s,%s,%s,%s,%s,%s,%s)"
#order=("sth",3643387,"sftqa2","AMEX")
#mycursor.execute(sqlformula,order)
mycursor.execute(sqlformula,(fulfilment,sku,environment,payment,orderNumber,date,username))
mydb.commit()
mydb.close()
输出
下面的输出中,我需要获得松弛的用户名,但我会收到随机字母
spu
3646989
mq1
AMEX
WLCLZ2LDP
"200002086748"
修改后的代码
@slack.RTMClient.run_on(event='message')
def say_hello(**payload):
try:
##0 get clients and payload
logging.info('msg received')
data = payload['data']
web_client = payload['web_client']
rtm_client = payload['rtm_client']
##0 - 1 Check if it is the first msg, not replied msg by me
# print(data)
if data.get('text') == None:
logging.info('This msg is my replied msg.')
return False
##0-2 Get channel info
channel_id = data['channel']
thread_ts = data['ts']
global user
user = data['user']
msg = data['text']
retVal = analysis_msg(msg)
response = web_client.users_list()
assert(response['ok'])
user_map = {x['id']: x['name'] for x in response['members']}
global user_name
user_name = user_map[user] if user in user_map else None
print(user_name)
情况1::已编辑代码的输出-当其他用户尝试从其计算机获取“ None”(不是预期的情况
spu
2469048
sftqa3
AMEX
None
BBY01-200002096736
情况2::编辑代码的输出:当我从机器上运行时,我能够看到我的帮助,即A6002043(预期)
spu
2469048
sftqa3
VISA
a6002043
BBY01-200002096040
答案 0 :(得分:2)
请允许我澄清两个误会:
要获取用户名,您需要调用API方法users.list来获取用户列表。这样,您就可以将ID映射到用户名。
这是一个示例代码,该示例代码如何获取用户ID的用户名:
slack_token = os.environ['SLACK_TOKEN']
response = client.users_list()
assert(response['ok'])
user_map = {x['id']: x['name'] for x in response['members']}
user_name = user_map[user_id] if user_id in user_map else None
print(user_name)