如何使用 twilio 在聊天程序中附加名称和消息?
How to append name with messages in chat program by using twilio?
我正在 angular 中使用 twilio api 创建一个聊天网络应用程序。目前我的聊天正在显示,用户可以发送消息,但我想显示当前正在发送消息的用户。
以下是我对 twilio
的 http post 请求
viewMessages():Observable<any>{
return
this.http.get("https://chat.twilio.com/v2/Services/"+
this.serviceId+"/Channels/"+this.myChannelId+
"/Messages",this.httpOpt).pipe(map(data=>data));
}
以下是类型脚本函数
name=localStorage.getItem('name');
allMessages=[];
totalMessages:number;
//View all messages
viewMessage(){
console.log(this.name);
this.chatBox.viewMessages().subscribe(res=>{
this.allMessages=res.messages
},
err=>{
console.log(err);
})
}
然后我通过插值显示名称。
您可以循环消息数组并将名称附加到它。
this.allMessages.forEach((message, index)=>{
return this.allMessages[index] = `${this.name}: ${message}`;
});
console.log(this.allMessages);
我正在 angular 中使用 twilio api 创建一个聊天网络应用程序。目前我的聊天正在显示,用户可以发送消息,但我想显示当前正在发送消息的用户。
以下是我对 twilio
的 http post 请求 viewMessages():Observable<any>{
return
this.http.get("https://chat.twilio.com/v2/Services/"+
this.serviceId+"/Channels/"+this.myChannelId+
"/Messages",this.httpOpt).pipe(map(data=>data));
}
以下是类型脚本函数
name=localStorage.getItem('name');
allMessages=[];
totalMessages:number;
//View all messages
viewMessage(){
console.log(this.name);
this.chatBox.viewMessages().subscribe(res=>{
this.allMessages=res.messages
},
err=>{
console.log(err);
})
}
然后我通过插值显示名称。
您可以循环消息数组并将名称附加到它。
this.allMessages.forEach((message, index)=>{
return this.allMessages[index] = `${this.name}: ${message}`;
});
console.log(this.allMessages);