第一步,在你想要创建机器人的群里面点击“群助手”
第二步 点击“添加机器人”
第三步 选择自定义
第四步 填写机器人的基本信息:设置头像,填写机器人名称,勾选安全设置
第五步 点击“完成”,获取webhook,webhook就是一个请求接口,我们之后会向这个接口发送POST请求,发送我们想要让机器人发送的消息。
第六步 编写代码,新建一个test.py文件,我的python是3.8
#python 3.8
import time
import hmac
import hashlib
import base64
import urllib.parse
import requests
timestamp = str(round(time.time() * 1000))
secret = '' //选择安全设置的时候,勾选“加签”,自动生成的secret,复制粘贴到这里
secret_enc = secret.encode('utf-8')
string_to_sign = '{}\n{}'.format(timestamp, secret)
string_to_sign_enc = string_to_sign.encode('utf-8')
hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))
print(timestamp)
print(sign)
web_hook = '' //新建机器人成功后,生成的webhook,复制粘贴到这里
url = web_hook+'×tamp='+timestamp+'&sign='+sign
HEADERS = {'Content-Type': 'application/json'}
data = {
"msgtype": "text",
"text": {
"content": "这是一个消息,群机器人新建成功了,这里随便输入你想要发送的消息"
}
}
res = requests.post(url=url, headers=HEADERS, json=data)
print(res.text)
第七步 运行此文件,你就会发现群机器人成功发送消息到群里了。