hanxi / xiaomusic

使用小爱音箱播放音乐,音乐使用 yt-dlp 下载。
https://github.com/hanxi/xiaomusic
MIT License
903 stars 89 forks source link

【高级篇】自定义口令功能 #105

Open hanxi opened 4 days ago

hanxi commented 4 days ago

自定义口令配置需要配置到 config.json 文件里,使用 config.json 方式启动。参考 https://github.com/hanxi/xiaomusic/issues/94

口令的配置方式见 config-example.json 文件。口令对应的代码需要写到 plugins/ 目录下面,如果是容器启动,则需要把这个目录挂载出来。

config.json 格式是下面这样的。

{
    "hardware": "L07A",
    "account": "",
    "password": "",
    "mi_did": "",
    "cookie": "",
    "verbose": false,
    "music_path": "music",
    "conf_path": null,
    "hostname": "192.168.2.5",
    "port": 8090,
    "public_port": 0,
    "proxy": null,
    "search_prefix": "bilisearch:",
    "ffmpeg_location": "./ffmpeg/bin",
    "active_cmd": "play,random_play,playlocal,play_music_list,stop",
    "exclude_dirs": "@eaDir",
    "music_path_depth": 10,
    "disable_httpauth": true,
    "httpauth_username": "admin",
    "httpauth_password": "admin",
    "music_list_url": "",
    "music_list_json": "",
    "disable_download": false,
    "key_word_dict": {
        "播放歌曲": "play",
        "播放本地歌曲": "playlocal",
        "关机": "stop",
        "下一首": "play_next",
        "单曲循环": "set_play_type_one",
        "全部循环": "set_play_type_all",
        "随机播放": "random_play",
        "分钟后关机": "stop_after_minute",
        "播放列表": "play_music_list",
        "刷新列表": "gen_music_list",
        "set_volume#": "set_volume",
        "get_volume#": "get_volume",
        "本地播放歌曲": "playlocal",
        "放歌曲": "play",
        "暂停": "stop",
        "停止": "stop",
        "停止播放": "stop",
        "测试自定义口令": "exec#code1(\"hello\")",
        "测试链接": "exec#httpget(\"https://github.com/hanxi/xiaomusic\")"
    },
    "key_match_order": [
        "set_volume#",
        "get_volume#",
        "分钟后关机",
        "播放歌曲",
        "下一首",
        "单曲循环",
        "全部循环",
        "随机播放",
        "关机",
        "刷新列表",
        "播放列表",
        "播放本地歌曲",
        "本地播放歌曲",
        "放歌曲",
        "暂停",
        "停止",
        "停止播放",
        "测试自定义口令",
        "测试链接"
    ],
    "use_music_api": false,
    "use_music_audio_id": "1582971365183456177",
    "use_music_id": "355454500",
    "log_file": "/tmp/xiaomusic.txt",
    "fuzzy_match_cutoff": 0.6,
    "enable_fuzzy_match": true,
    "stop_tts_msg": "收到,再见",
    "keywords_playlocal": "播放本地歌曲,本地播放歌曲",
    "keywords_play": "播放歌曲,放歌曲",
    "keywords_stop": "关机,暂停,停止,停止播放",
    "user_key_word_dict": {
        "测试自定义口令": "exec#code1(\"hello\")",
        "测试链接": "exec#httpget(\"https://github.com/hanxi/xiaomusic\")"
    }
}

配置自定义口令时,只需要配置 user_key_word_dict 即可,会自动插入到 key_word_dict 里的。配置格式是:

 "测试自定义口令": "exec#code1(\"hello\")",

其中 "测试自定义口令" 就是对小爱音箱说的,"exec#code1(\"hello\")" 就是要执行的插件代码,代码以 exec# 开头,后面紧跟着执行代码。这里 code1 是一个插件函数,插件函数需要在 plugin 目录里实现,一个文件只会导出一个与文件名相同的插件函数。所以 code1 函数是在 plugin/code1.py 里实现的。

async def code1(arg1):
    global log, xiaomusic
    log.info(f"code1:{arg1}")
    await xiaomusic.do_tts("你好,我是自定义的测试口令")

这里只是演示了打印日志和让小爱音箱说话。还有一个示例插件是 httpget ,可以用来访问 url 。

比如下面这样配置的话,当对小爱音箱说测试链接时,会去访问 url ,可以用来很多其他的事情。

"测试链接": "exec#httpget(\"https://github.com/hanxi/xiaomusic\")

感兴趣的可以体验一下,写了有什么好玩的插件也可以在这里分享,或者提 pr 合并进官方库里作为自带插件。