要编写一个自定义动作,以较慢的速度说出常量提示语的几个单词,可以使用Python编写一个谷歌助手扩展程序。以下是一个示例代码,可以实现这个功能:
import time
from typing import Any, Dict, Optional
from google.assistant.embedded.v1alpha2 import embedded_assistant_pb2
from google.assistant.embedded.v1alpha2 import embedded_assistant_pb2_grpc
class SlowConstantsAction:
def __init__(self, language_code: str) -> None:
self.language_code = language_code
def run(self) -> None:
# 创建一个与谷歌助手的通信通道
with embedded_assistant_pb2_grpc.EmbeddedAssistantStub() as assistant:
# 创建一个请求
request = embedded_assistant_pb2.AssistRequest(
audio_in_config=embedded_assistant_pb2.AudioInConfig(
encoding='LINEAR16',
sample_rate_hertz=16000,
),
audio_out_config=embedded_assistant_pb2.AudioOutConfig(
encoding='LINEAR16',
sample_rate_hertz=16000,
volume_percentage=50,
pitch=-2.0, # 降低音调来实现较慢的说话速度
speaking_rate=0.8, # 设置说话速度为0.8倍
),
device_config=embedded_assistant_pb2.DeviceConfig(
device_id='your_device_id',
device_model_id='your_device_model_id',
),
dialog_state_in=embedded_assistant_pb2.DialogStateIn(
language_code=self.language_code,
),
query=embedded_assistant_pb2.Query(
text='const1 const2 const3', # 声明你的常量提示语
),
)
# 发送请求并接收响应
response = assistant.Assist(request)
# 处理响应
if response.dialog_state_out.supplemental_display_text:
print('Assistant says:', response.dialog_state_out.supplemental_display_text)
# 在主函数中运行自定义动作
if __name__ == '__main__':
action = SlowConstantsAction('en-US')
action.run()
请注意,此示例代码仅为演示目的,实际情况中,你需要将your_device_id
和your_device_model_id
替换为你的设备ID和设备模型ID,并根据需要调整常量提示语的内容。
此代码使用gRPC库与谷歌助手进行通信,并通过设置pitch
和speaking_rate
参数来降低音调和减慢说话速度。