基于腾讯云函数&企业微信应用的python小程序:每日英语(英语考级党必备)

基于腾讯云函数&企业微信应用的python小程序:每日英语(英语考级党必备),第1张

基于腾讯云函数&企业微信应用的python小程序:每日英语(英语考级党必备)

每日英语主要调用的是爱词霸的接口,网上已经有很多代码了,这边主要是结合企业微信使用,达到以下效果:

1.每日推送到企业微信应用,通过卡片消息的方式;

2.卡片点进去就是音频文件方便学习发音,非常适合考英语四六级的朋友,哈哈哈~

 腾讯云执行方法:index.main_handler

import requests
import json
import urllib.request
# 获取爱词霸每日一句
def get_iciba_everyday():
    url = 'http://open.iciba.com/dsapi/'
    r = requests.get(url)
    json_response = r.json()
    try:
        day = json_response["dateline"] # 时间
        note1 = json_response["content"] #英文
        note2 = json_response["note"] #中译
        tts = json_response["tts"] #音频
    except:
        day = "error"
        note1 = "error"
        note2 = "error"
        tts = "error"
    print(str(day)+"n"+str(note1)+"n"+str(note2)+"n"+str(tts))
    return day, note1, note2, tts
# 发送消息
def send_msg():
    day, note1, note2, tts = get_iciba_everyday()
    cardtxt = "{}{}".format(str(note1),str(note2))
    print(cardtxt)
    Msg = {
            "title": str(day),
            "description": cardtxt,
            "url": str(tts),
            "btntxt": "更多"
        }
    ID = "" #你的企业ID
    SECRET = "" #你的应用secret
    AppID = 1000002 #你的应用ID
    wx_get_url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={}&corpsecret={}".format(ID, SECRET)
    ACCESS_TOKEN = json.loads(urllib.request.urlopen(wx_get_url).read())['access_token']
    wx_post_url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={}".format(ACCESS_TOKEN)
    post_data = {"touser": "@all", "chatid": "3", "msgtype": "textcard", "agentid": AppID, "textcard":Msg, "safe": 0, "enable_id_trans": 0, "enable_duplicate_check": 0}
    json_post_data = json.dumps(post_data, skipkeys=False, ensure_ascii=False)
    # urllib.request.urlopen(wx_post_url, json_post_data.encode(encoding='UTF8'))
    a = requests.post(wx_post_url, json_post_data.encode(encoding='UTF8'))
    print(a.text)

def main():
    send_msg()
def main_handler(event, context):
    return main()
if __name__ == '__main__':
    main()

欢迎关注,欢迎大佬指出问题或者交流提供更优的方案~

---------

 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5521881.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-13
下一篇 2022-12-13

发表评论

登录后才能评论

评论列表(0条)

保存