1
0
mirror of https://gitlab.com/xmpp-rs/xmpp-rs.git synced 2024-06-09 09:44:03 +02:00
xmpp-rs/xmpp/src/message/send.rs

45 lines
1.4 KiB
Rust

// Copyright (c) 2023 xmpp-rs contributors.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
use tokio_xmpp::connect::ServerConnector;
use tokio_xmpp::{
parsers::message::{Body, Message, MessageType},
Error, Jid,
};
use crate::Agent;
/// Send a message to a given recipient.
///
/// Returns the generated message ID.
///
/// # Arguments
///
/// * `agent`: The agent to use to send the message.
/// * `recipient`: The JID of the recipient.
/// * `type_`: The type of message to send.
/// For a message to a MUC room, this should be `MessageType::Groupchat`.
/// For a private message to a MUC room member or regular contact,
/// this should be `MessageType::Chat`.
/// * `lang`: The language of the message. (See IETF RFC 5646)
/// * `text`: The text of the message.
///
/// Returns the generated message ID, or an error if the message could not be sent.
pub async fn send_message<C: ServerConnector>(
agent: &mut Agent<C>,
recipient: Jid,
type_: MessageType,
lang: &str,
text: &str,
) -> Result<String, Error> {
let mut message = Message::new(Some(recipient));
message.type_ = type_;
message
.bodies
.insert(String::from(lang), Body(String::from(text)));
agent.client.send_stanza(message.into()).await
}