1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902
#![deny(missing_docs)]
#![deny(clippy::all, clippy::pedantic, clippy::nursery)]
// clippy::use_self fixed in https://github.com/rust-lang/rust-clippy/pull/9454
// TODO: remove this when clippy bug fixed in stable
#![allow(clippy::use_self)]
// should support this in the future - would be nice if all futures were send
#![allow(clippy::future_not_send)]
#![allow(clippy::missing_errors_doc)]
#![allow(clippy::option_if_let_else)]
#![cfg_attr(test, deny(warnings))]
#![doc(html_root_url = "https://docs.rs/webex/latest/webex/")]
//! # webex-rust
//!
//! A minimal asynchronous interface to Webex Teams, intended for (but not
//! limited to) implementing bots.
//!
//! Current functionality includes:
//!
//! - Registration with Webex APIs
//! - Monitoring an event stream
//! - Sending direct or group messages
//! - Getting room memberships
//! - Building `AdaptiveCards` and retrieving responses
//!
//! Not all features are fully-fleshed out, particularly the `AdaptiveCard`
//! support (only a few serializations exist, enough to create a form with a
//! few choices, a text box, and a submit button).
//!
//! # DISCLAIMER
//!
//! This crate is not maintained by Cisco, and not an official SDK. The
//! author is a current developer at Cisco, but has no direct affiliation
//! with the Webex development team.
#[macro_use]
extern crate error_chain;
extern crate lazy_static;
pub mod adaptive_card;
#[allow(missing_docs)]
pub mod error;
pub mod types;
pub use types::*;
pub mod auth;
use error::{Error, ErrorKind, ResultExt};
use crate::adaptive_card::AdaptiveCard;
use base64::{engine::general_purpose as bas64enc, Engine as _};
use futures::{future::try_join_all, try_join};
use futures_util::{SinkExt, StreamExt};
use hyper::{body::HttpBody, client::HttpConnector, Body, Client, Request};
use hyper_tls::HttpsConnector;
use log::{debug, error, trace, warn};
use serde::de::DeserializeOwned;
use std::{
collections::{hash_map::DefaultHasher, HashMap},
hash::{self, Hasher},
sync::Mutex,
time::Duration,
};
use tokio::net::TcpStream;
use tokio_tungstenite::{
connect_async,
tungstenite::{Error as TErr, Message as TMessage},
MaybeTlsStream, WebSocketStream,
};
/*
* URLs:
*
* https://help.webex.com/en-us/xbcr37/External-Connections-Made-by-the-Serviceability-Connector
*
* These apply to the central Webex Teams (Wxt) servers. WxT also supports enterprise servers;
* these are not supported.
*/
// Main API URL - default for any request.
const REST_HOST_PREFIX: &str = "https://api.ciscospark.com/v1";
// U2C - service discovery, used to discover other URLs (for example, the mercury URL).
const U2C_HOST_PREFIX: &str = "https://u2c.wbx2.com/u2c/api/v1";
// Default mercury URL, used when the token doesn't have permissions to list organizations.
const DEFAULT_REGISTRATION_HOST_PREFIX: &str = "https://wdm-a.wbx2.com/wdm/api/v1";
const CRATE_VERSION: &str = env!("CARGO_PKG_VERSION");
// Qualify webex devices created by this crate
const DEFAULT_DEVICE_NAME: &str = "rust-client";
const DEVICE_SYSTEM_NAME: &str = "rust-spark-client";
/// Web Socket Stream type
pub type WStream = WebSocketStream<MaybeTlsStream<TcpStream>>;
type WebClient = Client<HttpsConnector<HttpConnector>, Body>;
/// Webex API Client
#[derive(Clone)]
#[must_use]
pub struct Webex {
id: u64,
client: RestClient,
token: String,
/// Webex Device Information used for device registration
pub device: DeviceData,
}
/// Webex Event Stream handler
pub struct WebexEventStream {
ws_stream: WStream,
timeout: Duration,
/// Signifies if WebStream is Open
pub is_open: bool,
}
impl WebexEventStream {
/// Get the next event from an event stream
///
/// Returns an event or an error
///
/// # Errors
/// Returns an error when the underlying stream has a problem, but will
/// continue to work on subsequent calls to `next()` - the errors can safely
/// be ignored.
pub async fn next(&mut self) -> Result<Event, Error> {
loop {
let next = self.ws_stream.next();
match tokio::time::timeout(self.timeout, next).await {
// Timed out
Err(_) => {
// This does not seem to be recoverable, or at least there are conditions under
// which it does not recover. Indicate that the connection is closed and a new
// one will have to be opened.
self.is_open = false;
return Err(format!("no activity for at least {:?}", self.timeout).into());
}
// Didn't time out
Ok(next_result) => match next_result {
None => continue,
Some(msg) => match msg {
Ok(msg) => {
if let Some(h_msg) = self.handle_message(msg)? {
return Ok(h_msg);
}
// `None` messages still reset the timeout (e.g. Ping to keep alive)
}
Err(TErr::Protocol(_) | TErr::Io(_)) => {
// Protocol error probably requires a connection reset
// IO error is (apart from WouldBlock) generally an error with the
// underlying connection and also fatal
self.is_open = false;
return Err(msg.unwrap_err().to_string().into());
}
Err(e) => {
return Err(ErrorKind::Tungstenite(
e,
"Error getting next_result".into(),
)
.into())
}
},
},
}
}
}
fn handle_message(&mut self, msg: TMessage) -> Result<Option<Event>, Error> {
match msg {
TMessage::Binary(bytes) => {
let json = std::str::from_utf8(&bytes)?;
match serde_json::from_str(json) {
Ok(ev) => Ok(Some(ev)),
Err(e) => {
warn!("Couldn't deserialize: {:?}. Original JSON:\n{}", e, &json);
Err(e.into())
}
}
}
TMessage::Text(t) => {
debug!("text: {}", t);
Ok(None)
}
TMessage::Ping(_) => {
trace!("Ping!");
Ok(None)
}
TMessage::Close(t) => {
debug!("close: {:?}", t);
self.is_open = false;
Err(ErrorKind::Closed("Web Socket Closed".to_string()).into())
}
TMessage::Pong(_) => {
debug!("Pong!");
Ok(None)
}
TMessage::Frame(_) => {
debug!("Frame");
Ok(None)
}
}
}
pub(crate) async fn auth(ws_stream: &mut WStream, token: &str) -> Result<(), Error> {
/*
* Authenticate to the stream
*/
let auth = types::Authorization::new(token);
debug!("Authenticating to stream");
match ws_stream
.send(TMessage::Text(serde_json::to_string(&auth).unwrap()))
.await
{
Ok(()) => {
/*
* The next thing back should be a pong
*/
match ws_stream.next().await {
Some(msg) => match msg {
Ok(msg) => match msg {
TMessage::Ping(_) | TMessage::Pong(_) => {
debug!("Authentication succeeded");
Ok(())
}
_ => Err(format!("Received {msg:?} in reply to auth message").into()),
},
Err(e) => Err(format!("Received error from websocket: {e}").into()),
},
None => Err("Websocket closed".to_string().into()),
}
}
Err(e) => {
Err(ErrorKind::Tungstenite(e, "failed to send authentication".to_string()).into())
}
}
}
}
enum AuthorizationType<'a> {
None,
Bearer(&'a str),
Basic {
username: &'a str,
password: &'a str,
},
}
/// Implements low level REST requests to be used internally by the library
#[derive(Clone)]
struct RestClient {
host_prefix: HashMap<String, String>,
web_client: WebClient,
}
struct RequestBody<T>
where
Body: From<T>,
{
media_type: &'static str,
content: T,
}
impl RestClient {
/// Creates a new `RestClient`
pub fn new() -> Self {
let https = HttpsConnector::new();
let web_client = Client::builder().build::<_, hyper::Body>(https);
Self {
host_prefix: HashMap::new(),
web_client,
}
}
/******************************************************************
* Low-level API. These calls are chained to build various
* high-level calls like "get_message"
******************************************************************/
async fn api_get<'a, T: DeserializeOwned>(
&self,
rest_method: &str,
auth: AuthorizationType<'a>,
) -> Result<T, Error> {
let body: Option<RequestBody<String>> = None;
self.rest_api("GET", rest_method, body, auth).await
}
async fn api_delete<'a>(
&self,
rest_method: &str,
auth: AuthorizationType<'a>,
) -> Result<(), Error> {
let body: Option<RequestBody<String>> = None;
self.rest_api("DELETE", rest_method, body, auth).await
}
async fn api_post<'a, T: DeserializeOwned, U: Send>(
&self,
rest_method: &str,
body: RequestBody<U>,
auth: AuthorizationType<'a>,
) -> Result<T, Error>
where
Body: From<U>,
{
self.rest_api("POST", rest_method, Some(body), auth).await
}
async fn api_put<'a, T: DeserializeOwned, U: Send>(
&self,
rest_method: &str,
body: RequestBody<U>,
auth: AuthorizationType<'a>,
) -> Result<T, Error>
where
Body: From<U>,
{
self.rest_api("PUT", rest_method, Some(body), auth).await
}
async fn rest_api<'a, T: DeserializeOwned, U: Send>(
&self,
http_method: &str,
rest_method: &str,
body: Option<RequestBody<U>>,
auth: AuthorizationType<'a>,
) -> Result<T, Error>
where
Body: From<U>,
{
let reply = self
.call_web_api_raw(http_method, rest_method, body, auth)
.await?;
let mut reply_str = reply.as_str();
if reply_str.is_empty() {
reply_str = "null";
}
serde_json::from_str(reply_str).map_err(|e| {
error!("Couldn't parse reply for {} call: {:#?}", rest_method, e);
trace!("Source JSON: `{}`", reply_str);
Error::with_chain(e, "failed to parse reply")
})
}
async fn call_web_api_raw<'a, T: Send>(
&self,
http_method: &str,
rest_method: &str,
body: Option<RequestBody<T>>,
auth: AuthorizationType<'a>,
) -> Result<String, Error>
where
Body: From<T>,
{
let default_prefix = String::from(REST_HOST_PREFIX);
let rest_method_trimmed = rest_method.split('?').next().unwrap_or(rest_method);
let prefix = self
.host_prefix
.get(rest_method_trimmed)
.unwrap_or(&default_prefix);
let url = format!("{prefix}/{rest_method}");
debug!("Calling {} {:?}", http_method, url);
let mut builder = match auth {
AuthorizationType::None => Request::builder(),
AuthorizationType::Basic { username, password } => {
let encoded = bas64enc::STANDARD_NO_PAD.encode(format!("{username}:{password}"));
Request::builder().header("Authorization", format!("Basic {encoded}"))
}
AuthorizationType::Bearer(token) => {
Request::builder().header("Authorization", format!("Bearer {token}"))
}
}
.method(http_method)
.uri(url);
let body = match body {
None => Body::empty(),
Some(request_body) => {
builder = builder.header("Content-Type", request_body.media_type);
Body::from(request_body.content)
}
};
let req = builder.body(body).expect("request builder");
match self.web_client.request(req).await {
Ok(mut resp) => {
let mut reply = String::new();
while let Some(chunk) = resp.body_mut().data().await {
use std::str;
let chunk = chunk?;
let strchunk = str::from_utf8(&chunk)?;
reply.push_str(strchunk);
}
match resp.status() {
hyper::StatusCode::LOCKED | hyper::StatusCode::TOO_MANY_REQUESTS => {
let retry_after = resp
.headers()
.get("Retry-After")
.and_then(|s| s.to_str().ok())
.and_then(|t| t.parse::<i64>().ok());
warn!(
"Limited calling {} {}/{}",
http_method, prefix, rest_method_trimmed
);
debug!("Retry-After: {:?}", retry_after);
Err(ErrorKind::Limited(resp.status(), retry_after).into())
}
status if !status.is_success() => {
Err(ErrorKind::StatusText(resp.status(), reply).into())
}
_ => Ok(reply),
}
}
Err(e) => Err(Error::with_chain(e, "request failed")),
}
}
}
impl Webex {
/// Constructs a new Webex Teams context from a token
/// Tokens can be obtained when creating a bot, see <https://developer.webex.com/my-apps> for
/// more information and to create your own Webex bots.
pub async fn new(token: &str) -> Self {
Self::new_with_device_name(DEFAULT_DEVICE_NAME, token).await
}
/// Constructs a new Webex Teams context from a token and a chosen name
/// The name is used to identify the device/client with Webex api
pub async fn new_with_device_name(device_name: &str, token: &str) -> Self {
let https = HttpsConnector::new();
let web_client = Client::builder().build::<_, hyper::Body>(https);
let mut client: RestClient = RestClient {
host_prefix: HashMap::new(),
web_client,
};
let mut hasher = DefaultHasher::new();
hash::Hash::hash_slice(token.as_bytes(), &mut hasher);
let id = hasher.finish();
// Have to insert this before calling get_mercury_url() since it uses U2C for the catalog
// request.
client
.host_prefix
.insert("limited/catalog".to_string(), U2C_HOST_PREFIX.to_string());
let mut webex = Self {
id,
client,
token: token.to_string(),
device: DeviceData {
device_name: Some(DEFAULT_DEVICE_NAME.to_string()),
device_type: Some("DESKTOP".to_string()),
localized_model: Some("rust".to_string()),
model: Some(format!("rust-v{CRATE_VERSION}")),
name: Some(device_name.to_owned()),
system_name: Some(DEVICE_SYSTEM_NAME.to_string()),
system_version: Some(CRATE_VERSION.to_string()),
..DeviceData::default()
},
};
let devices_url = match webex.get_mercury_url().await {
Ok(url) => {
trace!("Fetched mercury url {}", url);
url
}
Err(e) => {
debug!("Failed to fetch devices url, falling back to default");
debug!("Error: {:?}", e);
DEFAULT_REGISTRATION_HOST_PREFIX.to_string()
}
};
webex
.client
.host_prefix
.insert("devices".to_string(), devices_url);
webex
}
/// Get an event stream handle
pub async fn event_stream(&self) -> Result<WebexEventStream, Error> {
// Helper function to connect to a device
// refactored out to make it easier to loop through all devices and also lazily create a
// new one if needed
async fn connect_device(s: &Webex, device: DeviceData) -> Result<WebexEventStream, Error> {
trace!("Attempting connection with device named {:?}", device.name);
let Some(ws_url) = device.ws_url else {
return Err("Device has no ws_url".into());
};
let url = url::Url::parse(ws_url.as_str())
.map_err(|_| Error::from("Failed to parse ws_url"))?;
debug!("Connecting to {:?}", url);
match connect_async(url.clone()).await {
Ok((mut ws_stream, _response)) => {
debug!("Connected to {}", url);
WebexEventStream::auth(&mut ws_stream, &s.token).await?;
debug!("Authenticated");
let timeout = Duration::from_secs(20);
Ok(WebexEventStream {
ws_stream,
timeout,
is_open: true,
})
}
Err(e) => {
warn!("Failed to connect to {:?}: {:?}", url, e);
Err(ErrorKind::Tungstenite(e, "Failed to connect to ws_url".to_string()).into())
}
}
}
// get_devices automatically tries to set up devices if the get fails.
// Keep only devices named DEVICE_NAME to avoid conflicts with other clients
let mut devices: Vec<DeviceData> = self
.get_devices()
.await?
.iter()
.filter(|d| d.name == self.device.name)
.inspect(|d| trace!("Kept device: {}", d))
.cloned()
.collect();
// Sort devices in descending order by modification time, meaning latest created device
// first.
devices.sort_by(|a: &DeviceData, b: &DeviceData| {
b.modification_time
.unwrap_or_else(chrono::Utc::now)
.cmp(&a.modification_time.unwrap_or_else(chrono::Utc::now))
});
for device in devices {
if let Ok(event_stream) = connect_device(self, device).await {
trace!("Successfully connected to device.");
return Ok(event_stream);
}
}
// Failed to connect to any existing devices, creating new one
connect_device(self, self.setup_devices().await?).await
}
async fn get_mercury_url(&self) -> Result<String, Option<error::Error>> {
// Bit of a hacky workaround, error::Error does not implement clone
// TODO: this can be fixed by returning a Result<String, &error::Error>
lazy_static::lazy_static! {
static ref MERCURY_CACHE: Mutex<HashMap<u64, Result<String, ()>>> = Mutex::new(HashMap::new());
}
if let Ok(Some(result)) = MERCURY_CACHE
.lock()
.map(|cache| cache.get(&self.id).map(Clone::clone))
{
trace!("Found mercury URL in cache!");
return result.map_err(|()| None);
}
let mercury_url = self.get_mercury_url_uncached().await;
if let Ok(mut cache) = MERCURY_CACHE.lock() {
let result = mercury_url.as_ref().map_or(Err(()), |url| Ok(url.clone()));
trace!("Saving mercury url to cache: {}=>{:?}", self.id, &result);
cache.insert(self.id, result);
}
mercury_url.map_err(Some)
}
async fn get_mercury_url_uncached(&self) -> Result<String, error::Error> {
// Steps:
// 1. Get org id by GET /v1/organizations
// 2. Get urls json from https://u2c.wbx2.com/u2c/api/v1/limited/catalog?orgId=[org id]
// 3. mercury url is urls["serviceLinks"]["wdm"]
//
// 4. Add caching because this doesn't change, and it can be slow
let orgs = self.list::<Organization>().await?;
if orgs.is_empty() {
return Err("Can't get mercury URL with no orgs".into());
}
let org_id = &orgs[0].id;
let api_url = format!("limited/catalog?format=hostmap&orgId={org_id}");
let catalogs = self
.client
.api_get::<CatalogReply>(&api_url, AuthorizationType::Bearer(&self.token))
.await?;
let mercury_url = catalogs.service_links.wdm;
Ok(mercury_url)
}
/// Get list of organizations
#[deprecated(
since = "0.6.3",
note = "Please use `webex::list::<Organization>()` instead"
)]
pub async fn get_orgs(&self) -> Result<Vec<Organization>, Error> {
self.list().await
}
/// Get attachment action
/// Retrieves the attachment for the given ID. This can be used to
/// retrieve data from an `AdaptiveCard` submission
#[deprecated(
since = "0.6.3",
note = "Please use `webex::get::<AttachmentAction>(id)` instead"
)]
pub async fn get_attachment_action(&self, id: &GlobalId) -> Result<AttachmentAction, Error> {
self.get(id).await
}
/// Get a message by ID
#[deprecated(
since = "0.6.3",
note = "Please use `webex::get::<Message>(id)` instead"
)]
pub async fn get_message(&self, id: &GlobalId) -> Result<Message, Error> {
self.get(id).await
}
/// Delete a message by ID
#[deprecated(
since = "0.6.3",
note = "Please use `webex::delete::<Message>(id)` instead"
)]
pub async fn delete_message(&self, id: &GlobalId) -> Result<(), Error> {
self.delete::<Message>(id).await
}
/// Get available rooms
#[deprecated(since = "0.6.3", note = "Please use `webex::list::<Room>()` instead")]
pub async fn get_rooms(&self) -> Result<Vec<Room>, Error> {
self.list().await
}
/// Get all rooms from all organizations that the client belongs to.
/// Will be slow as does multiple API calls (one to get teamless rooms, one to get teams, then
/// one per team).
pub async fn get_all_rooms(&self) -> Result<Vec<Room>, Error> {
let (mut all_rooms, teams) = try_join!(self.list(), self.list::<Team>())?;
let team_endpoints: Vec<_> = teams
.into_iter()
.map(|team| format!("{}/?teamId={}", Room::API_ENDPOINT, team.id))
.collect();
let futures: Vec<_> = team_endpoints
.iter()
.map(|endpoint| {
self.client
.api_get::<ListResult<Room>>(endpoint, AuthorizationType::Bearer(&self.token))
})
.collect();
let teams_rooms = try_join_all(futures).await?;
for room in teams_rooms {
all_rooms.extend(room.items);
}
Ok(all_rooms)
}
/// Get available room
#[deprecated(since = "0.6.3", note = "Please use `webex::get::<Room>(id)` instead")]
pub async fn get_room(&self, id: &GlobalId) -> Result<Room, Error> {
self.get(id).await
}
/// Get information about person
#[deprecated(
since = "0.6.3",
note = "Please use `webex::get::<Person>(id)` instead"
)]
pub async fn get_person(&self, id: &GlobalId) -> Result<Person, Error> {
self.get(id).await
}
/// Send a message to a user or room
///
/// # Arguments
/// * `message`: [`MessageOut`] - the message to send, including one of `room_id`,
/// `to_person_id` or `to_person_email`.
///
/// # Errors
/// Types of errors returned:
/// * [`ErrorKind::Limited`] - returned on HTTP 423/429 with an optional Retry-After.
/// * [`ErrorKind::Status`] | [`ErrorKind::StatusText`] - returned when the request results in a non-200 code.
/// * [`ErrorKind::Json`] - returned when your input object cannot be serialized, or the return
/// value cannot be deserialised. (If this happens, this is a library bug and should be
/// reported.)
/// * [`ErrorKind::UTF8`] - returned when the request returns non-UTF8 code.
pub async fn send_message(&self, message: &MessageOut) -> Result<Message, Error> {
self.client
.api_post(
"messages",
RequestBody {
media_type: "application/json",
content: serde_json::to_string(&message)?,
},
AuthorizationType::Bearer(&self.token),
)
.await
}
/// Edit an existing message
///
/// # Arguments
/// * `params`: [`MessageEditParams`] - the message to edit, including the message ID and the room ID,
/// as well as the new message text.
///
/// # Errors
/// Types of errors returned:
/// * [`ErrorKind::Limited`] - returned on HTTP 423/429 with an optional Retry-After.
/// * [`ErrorKind::Status`] | [`ErrorKind::StatusText`] - returned when the request results in a non-200 code.
/// * [`ErrorKind::Json`] - returned when your input object cannot be serialized, or the return
/// value cannot be deserialised. (If this happens, this is a library bug and should be reported).
pub async fn edit_message(
&self,
message_id: &GlobalId,
params: &MessageEditParams<'_>,
) -> Result<Message, Error> {
let rest_method = format!("messages/{}", message_id.id());
self.client
.api_put(
&rest_method,
RequestBody {
media_type: "application/json",
content: serde_json::to_string(¶ms)?,
},
AuthorizationType::Bearer(&self.token),
)
.await
}
/// Get a resource from an ID
/// # Errors
/// * [`ErrorKind::Limited`] - returned on HTTP 423/429 with an optional Retry-After.
/// * [`ErrorKind::Status`] | [`ErrorKind::StatusText`] - returned when the request results in a non-200 code.
/// * [`ErrorKind::Json`] - returned when your input object cannot be serialized, or the return
/// value cannot be deserialised. (If this happens, this is a library bug and should be
/// reported.)
/// * [`ErrorKind::UTF8`] - returned when the request returns non-UTF8 code.
pub async fn get<T: Gettable + DeserializeOwned>(&self, id: &GlobalId) -> Result<T, Error> {
let rest_method = format!("{}/{}", T::API_ENDPOINT, id.id());
self.client
.api_get::<T>(rest_method.as_str(), AuthorizationType::Bearer(&self.token))
.await
.chain_err(|| {
format!(
"Failed to get {} with id {:?}",
std::any::type_name::<T>(),
id
)
})
}
/// Delete a resource from an ID
pub async fn delete<T: Gettable + DeserializeOwned>(&self, id: &GlobalId) -> Result<(), Error> {
let rest_method = format!("{}/{}", T::API_ENDPOINT, id.id());
self.client
.api_delete(rest_method.as_str(), AuthorizationType::Bearer(&self.token))
.await
.chain_err(|| {
format!(
"Failed to delete {} with id {:?}",
std::any::type_name::<T>(),
id
)
})
}
/// List resources of a type
pub async fn list<T: Gettable + DeserializeOwned>(&self) -> Result<Vec<T>, Error> {
self.client
.api_get::<ListResult<T>>(T::API_ENDPOINT, AuthorizationType::Bearer(&self.token))
.await
.map(|result| result.items)
.chain_err(|| format!("Failed to list {}", std::any::type_name::<T>()))
}
/// List resources of a type, with parameters
pub async fn list_with_params<T: Gettable + DeserializeOwned>(
&self,
list_params: T::ListParams<'_>,
) -> Result<Vec<T>, Error> {
let rest_method = format!(
"{}?{}",
T::API_ENDPOINT,
serde_html_form::to_string(list_params)?
);
self.client
.api_get::<ListResult<T>>(&rest_method, AuthorizationType::Bearer(&self.token))
.await
.map(|result| result.items)
.chain_err(|| format!("Failed to list {}", std::any::type_name::<T>()))
}
async fn get_devices(&self) -> Result<Vec<DeviceData>, Error> {
match self
.client
.api_get::<DevicesReply>("devices", AuthorizationType::Bearer(&self.token))
.await
{
#[rustfmt::skip]
Ok(DevicesReply { devices: Some(devices), .. }) => Ok(devices),
Ok(DevicesReply { devices: None, .. }) => {
debug!("Chaining one-time device setup from devices query");
self.setup_devices().await.map(|device| vec![device])
}
Err(e) => match e {
Error(ErrorKind::Status(s) | ErrorKind::StatusText(s, _), _) => {
if s == hyper::StatusCode::NOT_FOUND {
debug!("No devices found, creating new one");
self.setup_devices().await.map(|device| vec![device])
} else {
Err(Error::with_chain(e, "Can't decode devices reply"))
}
}
Error(ErrorKind::Limited(_, _), _) => Err(e),
_ => Err(format!("Can't decode devices reply: {e}").into()),
},
}
}
async fn setup_devices(&self) -> Result<DeviceData, Error> {
trace!("Setting up new device: {}", &self.device);
self.client
.api_post(
"devices",
RequestBody {
media_type: "application/json",
content: serde_json::to_string(&self.device)?,
},
AuthorizationType::Bearer(&self.token),
)
.await
}
}
impl From<&AttachmentAction> for MessageOut {
fn from(action: &AttachmentAction) -> Self {
Self {
room_id: action.room_id.clone(),
..Self::default()
}
}
}
impl From<&Message> for MessageOut {
fn from(msg: &Message) -> Self {
let mut new_msg = Self::default();
if msg.room_type == Some(RoomType::Group) {
new_msg.room_id = msg.room_id.clone();
} else if let Some(person_id) = &msg.person_id {
new_msg.to_person_id = Some(person_id.clone());
} else {
new_msg.to_person_email = msg.person_email.clone();
}
new_msg
}
}
impl Message {
/// Reply to a message.
/// Posts the reply in the same chain as the replied-to message.
/// Contrast with [`MessageOut::from()`] which only replies in the same room.
#[must_use]
pub fn reply(&self) -> MessageOut {
MessageOut {
room_id: self.room_id.clone(),
parent_id: self
.parent_id
.as_deref()
.or(self.id.as_deref())
.map(ToOwned::to_owned),
..Default::default()
}
}
}
impl MessageOut {
/// Generates a new outgoing message from an existing message
///
/// # Arguments
///
/// * `msg` - the template message
///
/// Use `from_msg` to create a reply from a received message.
#[deprecated(since = "0.2.0", note = "Please use the from instead")]
#[must_use]
pub fn from_msg(msg: &Message) -> Self {
Self::from(msg)
}
/// Add attachment to an existing message
///
/// # Arguments
///
/// * `card` - Adaptive Card to attach
pub fn add_attachment(&mut self, card: AdaptiveCard) -> &Self {
self.attachments = Some(vec![Attachment {
content_type: "application/vnd.microsoft.card.adaptive".to_string(),
content: card,
}]);
self
}
}