admin管理员组文章数量:1559753
1、右键群,点击添加机器人
2、查看机器人资料,可以看到一个Webhook地址,这个地址之后有大用处
3、前往腾讯云,注册好账号,然后进入serverless服务中,地址:
https://console.cloud.tencent/scf/list-detail?rid=1&ns=default&id=helloworld-1625125268&menu=trigger&tab=codeTab
新建一个函数,模板选择自定义创建
函数代码中复制如下代码:
# -*- coding: utf8 -*-
import logging
import datetime
import time
import requests
import json
import pytz
robot_url = "https://qyapi.weixin.qq。。。。。。。。。。。。。。。。。。。"
# 向机器人推送消息,默认是 text(机器人支持 markdown)
metioned_userid = ["xxxxx"]
def post_to_robot(req: str, url=robot_url, msgtypePara="text"):
logging.info("Post to robot", req)
dictPush = {"msgtype": msgtypePara, msgtypePara: {"content": req}
,"text": { "content": "已经"+str(GetNowTimeHumanable())[0:19]+"了,头哥该上班啦!!!"
,"mentioned_list":metioned_userid}
}
response = requests.post(
url=url,
headers={'Content-Type': 'application/json'},
data=json.dumps(dictPush), timeout=5
)
logging.info("PostToRobot:%s , %s", response, response.text)
def main_handler(event="", context=""):
' event context 是云函数要用的,里面没用到'
# 初始化日志
LOG_FORMAT = "%(asctime)s - %(levelname)s - %(message)s"
logging.basicConfig(level=logging.INFO, format=LOG_FORMAT)
logging.info("Runing")
time = str(GetNowTimeHumanable())
ans = ""
logging.info("ans: %s",ans)
post_to_robot(ans)
# 时间函数
def changeTimeToTimeZone(timeUnix: int, timeZone="Asia/Shanghai"):
"转换当前时间戳到 UTC + 8"
timeUnix = datetime.datetime.utcfromtimestamp(timeUnix)
timeUnix = timeUnix.replace(tzinfo=pytz.timezone('UTC'))
return timeUnix.astimezone(pytz.timezone(timeZone))
def GetNowTimeHumanable():
# 获取当前时间
dt = datetime.datetime.now(datetime.timezone.utc)
ans = int(dt.timestamp())
logging.debug("GetNowIntegralMinute(Humanable): %s",
changeTimeToTimeZone(ans))
return changeTimeToTimeZone(ans)
if __name__ == "__main__":
main_handler()
其中红色部分为你的机器人webhook地址,mentioned_userid为你要@的人的userid,"text"是你想要让机器人说的话,str(GetNowTimeHumanable())[0:19]函数为获取当前时间字符穿的前19位,这三个部分均可以自行修改。
点击部署后,进入触发管理
选择自定义触发周期,cron表达式规则,点击cron相关文档即可查看,我这里采用
0 0 8 * * 1-5 *
实现的功能为,每周1到周5的早上8点整进行消息推送,这个触发周期表达式你可以根据自己的需求进行修改。
定义好触发周期,提交后即可,一个函数可以定义多个触发器,同样,一个机器人的webhook可以应用到多个函数中,最终实现效果如下:
版权声明:本文标题:从0上线一个简单提醒功能的企业微信机器人bot(python3.6+腾讯云部署+cron触发) 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dongtai/1727423624a1113846.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论