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可以应用到多个函数中,最终实现效果如下:

 

本文标签: 腾讯机器人上线简单功能