#![allow(clippy::all, unused_imports, dead_code)]
use crate::{Actor, ActorBehavior, Message, Port};
use anyhow::{Error, Result};
use reflow_actor::{message::EncodableValue, ActorContext};
use reflow_actor_macro::actor;
use serde_json::{json, Value};
use std::collections::HashMap;
use std::time::Duration;
const BASE_URL: &str = "https://graph.microsoft.com/v1.0";
const ENV_KEY: &str = "MICROSOFT_TEAMS_API_KEY";
fn apply_auth(
config: &reflow_actor::ActorConfig,
mut builder: reqwest::RequestBuilder,
) -> Result<reqwest::RequestBuilder> {
let credential = config
.get_config_or_env(ENV_KEY)
.ok_or_else(|| anyhow::anyhow!("Missing env var: {}", ENV_KEY))?;
builder = builder.header("Authorization", format!("Bearer {}", credential));
Ok(builder)
}
#[actor(
MicrosoftTeamsSendMessageActor,
inports::<100>(team_id, channel_id, body),
outports::<50>(response, error),
state(MemoryState)
)]
pub async fn microsoft_teams_send_message(
context: ActorContext,
) -> Result<HashMap<String, Message>, Error> {
let inputs = context.get_payload();
let actor_config = context.get_config();
let mut endpoint = "/teams/{team-id}/channels/{channel-id}/messages".to_string();
if let Some(val) = inputs.get("team_id") {
endpoint = endpoint.replace("{{team-id}}", &super::message_to_str(val));
}
if let Some(val) = inputs.get("channel_id") {
endpoint = endpoint.replace("{{channel-id}}", &super::message_to_str(val));
}
let url = format!("{}{}", BASE_URL.trim_end_matches('/'), endpoint);
let client = reqwest::Client::builder()
.timeout(Duration::from_secs(30))
.build()?;
let mut builder = client.post(&url);
builder = builder.header("Content-Type", "application/json");
builder = apply_auth(actor_config, builder)?;
let mut body = serde_json::Map::new();
if let Some(val) = inputs.get("body") {
body.insert("body".to_string(), val.clone().into());
}
if !body.is_empty() {
builder = builder.json(&serde_json::Value::Object(body));
}
let mut output = HashMap::new();
match builder.send().await {
Ok(resp) => {
let status = resp.status().as_u16();
let headers: HashMap<String, String> = resp
.headers()
.iter()
.filter_map(|(k, v)| v.to_str().ok().map(|val| (k.to_string(), val.to_string())))
.collect();
let body_text = resp.text().await.unwrap_or_default();
let body_value: Value =
serde_json::from_str(&body_text).unwrap_or(Value::String(body_text));
output.insert(
"response".to_string(),
Message::object(EncodableValue::from(json!({
"status": status,
"headers": headers,
"body": body_value,
}))),
);
}
Err(e) => {
output.insert(
"error".to_string(),
Message::Error(
format!(
"POST /teams/{{team-id}}/channels/{{channel-id}}/messages failed: {}",
e
)
.into(),
),
);
}
}
Ok(output)
}
#[actor(
MicrosoftTeamsListTeamsActor,
inports::<100>(trigger),
outports::<50>(response, error),
state(MemoryState)
)]
pub async fn microsoft_teams_list_teams(
context: ActorContext,
) -> Result<HashMap<String, Message>, Error> {
let inputs = context.get_payload();
let actor_config = context.get_config();
let endpoint = "/me/joinedTeams".to_string();
let url = format!("{}{}", BASE_URL.trim_end_matches('/'), endpoint);
let client = reqwest::Client::builder()
.timeout(Duration::from_secs(30))
.build()?;
let mut builder = client.get(&url);
builder = builder.header("Content-Type", "application/json");
builder = apply_auth(actor_config, builder)?;
let mut output = HashMap::new();
match builder.send().await {
Ok(resp) => {
let status = resp.status().as_u16();
let headers: HashMap<String, String> = resp
.headers()
.iter()
.filter_map(|(k, v)| v.to_str().ok().map(|val| (k.to_string(), val.to_string())))
.collect();
let body_text = resp.text().await.unwrap_or_default();
let body_value: Value =
serde_json::from_str(&body_text).unwrap_or(Value::String(body_text));
output.insert(
"response".to_string(),
Message::object(EncodableValue::from(json!({
"status": status,
"headers": headers,
"body": body_value,
}))),
);
}
Err(e) => {
output.insert(
"error".to_string(),
Message::Error(format!("GET /me/joinedTeams failed: {}", e).into()),
);
}
}
Ok(output)
}
#[actor(
MicrosoftTeamsCreateTeamActor,
inports::<100>(displayName, description, visibility, memberSettings, messagingSettings, funSettings, template_odata_bind),
outports::<50>(response, error),
state(MemoryState)
)]
pub async fn microsoft_teams_create_team(
context: ActorContext,
) -> Result<HashMap<String, Message>, Error> {
let inputs = context.get_payload();
let actor_config = context.get_config();
let endpoint = "/teams".to_string();
let url = format!("{}{}", BASE_URL.trim_end_matches('/'), endpoint);
let client = reqwest::Client::builder()
.timeout(Duration::from_secs(30))
.build()?;
let mut builder = client.post(&url);
builder = builder.header("Content-Type", "application/json");
builder = apply_auth(actor_config, builder)?;
let mut body = serde_json::Map::new();
if let Some(val) = inputs.get("displayName") {
body.insert("displayName".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("description") {
body.insert("description".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("visibility") {
body.insert("visibility".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("memberSettings") {
body.insert("memberSettings".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("messagingSettings") {
body.insert("messagingSettings".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("funSettings") {
body.insert("funSettings".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("template_odata_bind") {
body.insert("template@odata.bind".to_string(), val.clone().into());
}
if !body.is_empty() {
builder = builder.json(&serde_json::Value::Object(body));
}
let mut output = HashMap::new();
match builder.send().await {
Ok(resp) => {
let status = resp.status().as_u16();
let headers: HashMap<String, String> = resp
.headers()
.iter()
.filter_map(|(k, v)| v.to_str().ok().map(|val| (k.to_string(), val.to_string())))
.collect();
let body_text = resp.text().await.unwrap_or_default();
let body_value: Value =
serde_json::from_str(&body_text).unwrap_or(Value::String(body_text));
output.insert(
"response".to_string(),
Message::object(EncodableValue::from(json!({
"status": status,
"headers": headers,
"body": body_value,
}))),
);
}
Err(e) => {
output.insert(
"error".to_string(),
Message::Error(format!("POST /teams failed: {}", e).into()),
);
}
}
Ok(output)
}
#[actor(
MicrosoftTeamsReadTeamActor,
inports::<100>(team_id, select, expand),
outports::<50>(response, error),
state(MemoryState)
)]
pub async fn microsoft_teams_read_team(
context: ActorContext,
) -> Result<HashMap<String, Message>, Error> {
let inputs = context.get_payload();
let actor_config = context.get_config();
let mut endpoint = "/teams/{team-id}".to_string();
if let Some(val) = inputs.get("team_id") {
endpoint = endpoint.replace("{{team-id}}", &super::message_to_str(val));
}
let url = format!("{}{}", BASE_URL.trim_end_matches('/'), endpoint);
let client = reqwest::Client::builder()
.timeout(Duration::from_secs(30))
.build()?;
let mut builder = client.get(&url);
builder = builder.header("Content-Type", "application/json");
builder = apply_auth(actor_config, builder)?;
let mut query_pairs: Vec<(&str, String)> = Vec::new();
if let Some(val) = inputs.get("select") {
query_pairs.push(("$select", super::message_to_str(val)));
}
if let Some(val) = inputs.get("expand") {
query_pairs.push(("$expand", super::message_to_str(val)));
}
if !query_pairs.is_empty() {
builder = builder.query(&query_pairs);
}
let mut output = HashMap::new();
match builder.send().await {
Ok(resp) => {
let status = resp.status().as_u16();
let headers: HashMap<String, String> = resp
.headers()
.iter()
.filter_map(|(k, v)| v.to_str().ok().map(|val| (k.to_string(), val.to_string())))
.collect();
let body_text = resp.text().await.unwrap_or_default();
let body_value: Value =
serde_json::from_str(&body_text).unwrap_or(Value::String(body_text));
output.insert(
"response".to_string(),
Message::object(EncodableValue::from(json!({
"status": status,
"headers": headers,
"body": body_value,
}))),
);
}
Err(e) => {
output.insert(
"error".to_string(),
Message::Error(format!("GET /teams/{{team-id}} failed: {}", e).into()),
);
}
}
Ok(output)
}
#[actor(
MicrosoftTeamsUpdateTeamActor,
inports::<100>(team_id, displayName, description, memberSettings, messagingSettings),
outports::<50>(response, error),
state(MemoryState)
)]
pub async fn microsoft_teams_update_team(
context: ActorContext,
) -> Result<HashMap<String, Message>, Error> {
let inputs = context.get_payload();
let actor_config = context.get_config();
let mut endpoint = "/teams/{team-id}".to_string();
if let Some(val) = inputs.get("team_id") {
endpoint = endpoint.replace("{{team-id}}", &super::message_to_str(val));
}
let url = format!("{}{}", BASE_URL.trim_end_matches('/'), endpoint);
let client = reqwest::Client::builder()
.timeout(Duration::from_secs(30))
.build()?;
let mut builder = client.patch(&url);
builder = builder.header("Content-Type", "application/json");
builder = apply_auth(actor_config, builder)?;
let mut body = serde_json::Map::new();
if let Some(val) = inputs.get("displayName") {
body.insert("displayName".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("description") {
body.insert("description".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("memberSettings") {
body.insert("memberSettings".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("messagingSettings") {
body.insert("messagingSettings".to_string(), val.clone().into());
}
if !body.is_empty() {
builder = builder.json(&serde_json::Value::Object(body));
}
let mut output = HashMap::new();
match builder.send().await {
Ok(resp) => {
let status = resp.status().as_u16();
let headers: HashMap<String, String> = resp
.headers()
.iter()
.filter_map(|(k, v)| v.to_str().ok().map(|val| (k.to_string(), val.to_string())))
.collect();
let body_text = resp.text().await.unwrap_or_default();
let body_value: Value =
serde_json::from_str(&body_text).unwrap_or(Value::String(body_text));
output.insert(
"response".to_string(),
Message::object(EncodableValue::from(json!({
"status": status,
"headers": headers,
"body": body_value,
}))),
);
}
Err(e) => {
output.insert(
"error".to_string(),
Message::Error(format!("PATCH /teams/{{team-id}} failed: {}", e).into()),
);
}
}
Ok(output)
}
#[actor(
MicrosoftTeamsDeleteTeamActor,
inports::<100>(group_id),
outports::<50>(response, error),
state(MemoryState)
)]
pub async fn microsoft_teams_delete_team(
context: ActorContext,
) -> Result<HashMap<String, Message>, Error> {
let inputs = context.get_payload();
let actor_config = context.get_config();
let mut endpoint = "/groups/{group-id}".to_string();
if let Some(val) = inputs.get("group_id") {
endpoint = endpoint.replace("{{group-id}}", &super::message_to_str(val));
}
let url = format!("{}{}", BASE_URL.trim_end_matches('/'), endpoint);
let client = reqwest::Client::builder()
.timeout(Duration::from_secs(30))
.build()?;
let mut builder = client.delete(&url);
builder = builder.header("Content-Type", "application/json");
builder = apply_auth(actor_config, builder)?;
let mut output = HashMap::new();
match builder.send().await {
Ok(resp) => {
let status = resp.status().as_u16();
let headers: HashMap<String, String> = resp
.headers()
.iter()
.filter_map(|(k, v)| v.to_str().ok().map(|val| (k.to_string(), val.to_string())))
.collect();
let body_text = resp.text().await.unwrap_or_default();
let body_value: Value =
serde_json::from_str(&body_text).unwrap_or(Value::String(body_text));
output.insert(
"response".to_string(),
Message::object(EncodableValue::from(json!({
"status": status,
"headers": headers,
"body": body_value,
}))),
);
}
Err(e) => {
output.insert(
"error".to_string(),
Message::Error(format!("DELETE /groups/{{group-id}} failed: {}", e).into()),
);
}
}
Ok(output)
}
#[actor(
MicrosoftTeamsListChannelsActor,
inports::<100>(team_id, filter, select),
outports::<50>(response, error),
state(MemoryState)
)]
pub async fn microsoft_teams_list_channels(
context: ActorContext,
) -> Result<HashMap<String, Message>, Error> {
let inputs = context.get_payload();
let actor_config = context.get_config();
let mut endpoint = "/teams/{team-id}/channels".to_string();
if let Some(val) = inputs.get("team_id") {
endpoint = endpoint.replace("{{team-id}}", &super::message_to_str(val));
}
let url = format!("{}{}", BASE_URL.trim_end_matches('/'), endpoint);
let client = reqwest::Client::builder()
.timeout(Duration::from_secs(30))
.build()?;
let mut builder = client.get(&url);
builder = builder.header("Content-Type", "application/json");
builder = apply_auth(actor_config, builder)?;
let mut query_pairs: Vec<(&str, String)> = Vec::new();
if let Some(val) = inputs.get("filter") {
query_pairs.push(("$filter", super::message_to_str(val)));
}
if let Some(val) = inputs.get("select") {
query_pairs.push(("$select", super::message_to_str(val)));
}
if !query_pairs.is_empty() {
builder = builder.query(&query_pairs);
}
let mut output = HashMap::new();
match builder.send().await {
Ok(resp) => {
let status = resp.status().as_u16();
let headers: HashMap<String, String> = resp
.headers()
.iter()
.filter_map(|(k, v)| v.to_str().ok().map(|val| (k.to_string(), val.to_string())))
.collect();
let body_text = resp.text().await.unwrap_or_default();
let body_value: Value =
serde_json::from_str(&body_text).unwrap_or(Value::String(body_text));
output.insert(
"response".to_string(),
Message::object(EncodableValue::from(json!({
"status": status,
"headers": headers,
"body": body_value,
}))),
);
}
Err(e) => {
output.insert(
"error".to_string(),
Message::Error(format!("GET /teams/{{team-id}}/channels failed: {}", e).into()),
);
}
}
Ok(output)
}
#[actor(
MicrosoftTeamsCreateChannelActor,
inports::<100>(team_id, displayName, description, membershipType, isFavoriteByDefault),
outports::<50>(response, error),
state(MemoryState)
)]
pub async fn microsoft_teams_create_channel(
context: ActorContext,
) -> Result<HashMap<String, Message>, Error> {
let inputs = context.get_payload();
let actor_config = context.get_config();
let mut endpoint = "/teams/{team-id}/channels".to_string();
if let Some(val) = inputs.get("team_id") {
endpoint = endpoint.replace("{{team-id}}", &super::message_to_str(val));
}
let url = format!("{}{}", BASE_URL.trim_end_matches('/'), endpoint);
let client = reqwest::Client::builder()
.timeout(Duration::from_secs(30))
.build()?;
let mut builder = client.post(&url);
builder = builder.header("Content-Type", "application/json");
builder = apply_auth(actor_config, builder)?;
let mut body = serde_json::Map::new();
if let Some(val) = inputs.get("displayName") {
body.insert("displayName".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("description") {
body.insert("description".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("membershipType") {
body.insert("membershipType".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("isFavoriteByDefault") {
body.insert("isFavoriteByDefault".to_string(), val.clone().into());
}
if !body.is_empty() {
builder = builder.json(&serde_json::Value::Object(body));
}
let mut output = HashMap::new();
match builder.send().await {
Ok(resp) => {
let status = resp.status().as_u16();
let headers: HashMap<String, String> = resp
.headers()
.iter()
.filter_map(|(k, v)| v.to_str().ok().map(|val| (k.to_string(), val.to_string())))
.collect();
let body_text = resp.text().await.unwrap_or_default();
let body_value: Value =
serde_json::from_str(&body_text).unwrap_or(Value::String(body_text));
output.insert(
"response".to_string(),
Message::object(EncodableValue::from(json!({
"status": status,
"headers": headers,
"body": body_value,
}))),
);
}
Err(e) => {
output.insert(
"error".to_string(),
Message::Error(format!("POST /teams/{{team-id}}/channels failed: {}", e).into()),
);
}
}
Ok(output)
}
#[actor(
MicrosoftTeamsReadChannelActor,
inports::<100>(team_id, channel_id),
outports::<50>(response, error),
state(MemoryState)
)]
pub async fn microsoft_teams_read_channel(
context: ActorContext,
) -> Result<HashMap<String, Message>, Error> {
let inputs = context.get_payload();
let actor_config = context.get_config();
let mut endpoint = "/teams/{team-id}/channels/{channel-id}".to_string();
if let Some(val) = inputs.get("team_id") {
endpoint = endpoint.replace("{{team-id}}", &super::message_to_str(val));
}
if let Some(val) = inputs.get("channel_id") {
endpoint = endpoint.replace("{{channel-id}}", &super::message_to_str(val));
}
let url = format!("{}{}", BASE_URL.trim_end_matches('/'), endpoint);
let client = reqwest::Client::builder()
.timeout(Duration::from_secs(30))
.build()?;
let mut builder = client.get(&url);
builder = builder.header("Content-Type", "application/json");
builder = apply_auth(actor_config, builder)?;
let mut output = HashMap::new();
match builder.send().await {
Ok(resp) => {
let status = resp.status().as_u16();
let headers: HashMap<String, String> = resp
.headers()
.iter()
.filter_map(|(k, v)| v.to_str().ok().map(|val| (k.to_string(), val.to_string())))
.collect();
let body_text = resp.text().await.unwrap_or_default();
let body_value: Value =
serde_json::from_str(&body_text).unwrap_or(Value::String(body_text));
output.insert(
"response".to_string(),
Message::object(EncodableValue::from(json!({
"status": status,
"headers": headers,
"body": body_value,
}))),
);
}
Err(e) => {
output.insert(
"error".to_string(),
Message::Error(
format!(
"GET /teams/{{team-id}}/channels/{{channel-id}} failed: {}",
e
)
.into(),
),
);
}
}
Ok(output)
}
#[actor(
MicrosoftTeamsUpdateChannelActor,
inports::<100>(team_id, channel_id, displayName, description),
outports::<50>(response, error),
state(MemoryState)
)]
pub async fn microsoft_teams_update_channel(
context: ActorContext,
) -> Result<HashMap<String, Message>, Error> {
let inputs = context.get_payload();
let actor_config = context.get_config();
let mut endpoint = "/teams/{team-id}/channels/{channel-id}".to_string();
if let Some(val) = inputs.get("team_id") {
endpoint = endpoint.replace("{{team-id}}", &super::message_to_str(val));
}
if let Some(val) = inputs.get("channel_id") {
endpoint = endpoint.replace("{{channel-id}}", &super::message_to_str(val));
}
let url = format!("{}{}", BASE_URL.trim_end_matches('/'), endpoint);
let client = reqwest::Client::builder()
.timeout(Duration::from_secs(30))
.build()?;
let mut builder = client.patch(&url);
builder = builder.header("Content-Type", "application/json");
builder = apply_auth(actor_config, builder)?;
let mut body = serde_json::Map::new();
if let Some(val) = inputs.get("displayName") {
body.insert("displayName".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("description") {
body.insert("description".to_string(), val.clone().into());
}
if !body.is_empty() {
builder = builder.json(&serde_json::Value::Object(body));
}
let mut output = HashMap::new();
match builder.send().await {
Ok(resp) => {
let status = resp.status().as_u16();
let headers: HashMap<String, String> = resp
.headers()
.iter()
.filter_map(|(k, v)| v.to_str().ok().map(|val| (k.to_string(), val.to_string())))
.collect();
let body_text = resp.text().await.unwrap_or_default();
let body_value: Value =
serde_json::from_str(&body_text).unwrap_or(Value::String(body_text));
output.insert(
"response".to_string(),
Message::object(EncodableValue::from(json!({
"status": status,
"headers": headers,
"body": body_value,
}))),
);
}
Err(e) => {
output.insert(
"error".to_string(),
Message::Error(
format!(
"PATCH /teams/{{team-id}}/channels/{{channel-id}} failed: {}",
e
)
.into(),
),
);
}
}
Ok(output)
}
#[actor(
MicrosoftTeamsDeleteChannelActor,
inports::<100>(team_id, channel_id),
outports::<50>(response, error),
state(MemoryState)
)]
pub async fn microsoft_teams_delete_channel(
context: ActorContext,
) -> Result<HashMap<String, Message>, Error> {
let inputs = context.get_payload();
let actor_config = context.get_config();
let mut endpoint = "/teams/{team-id}/channels/{channel-id}".to_string();
if let Some(val) = inputs.get("team_id") {
endpoint = endpoint.replace("{{team-id}}", &super::message_to_str(val));
}
if let Some(val) = inputs.get("channel_id") {
endpoint = endpoint.replace("{{channel-id}}", &super::message_to_str(val));
}
let url = format!("{}{}", BASE_URL.trim_end_matches('/'), endpoint);
let client = reqwest::Client::builder()
.timeout(Duration::from_secs(30))
.build()?;
let mut builder = client.delete(&url);
builder = builder.header("Content-Type", "application/json");
builder = apply_auth(actor_config, builder)?;
let mut output = HashMap::new();
match builder.send().await {
Ok(resp) => {
let status = resp.status().as_u16();
let headers: HashMap<String, String> = resp
.headers()
.iter()
.filter_map(|(k, v)| v.to_str().ok().map(|val| (k.to_string(), val.to_string())))
.collect();
let body_text = resp.text().await.unwrap_or_default();
let body_value: Value =
serde_json::from_str(&body_text).unwrap_or(Value::String(body_text));
output.insert(
"response".to_string(),
Message::object(EncodableValue::from(json!({
"status": status,
"headers": headers,
"body": body_value,
}))),
);
}
Err(e) => {
output.insert(
"error".to_string(),
Message::Error(
format!(
"DELETE /teams/{{team-id}}/channels/{{channel-id}} failed: {}",
e
)
.into(),
),
);
}
}
Ok(output)
}
#[actor(
MicrosoftTeamsListChannelMessagesActor,
inports::<100>(team_id, channel_id, top, skip, filter),
outports::<50>(response, error),
state(MemoryState)
)]
pub async fn microsoft_teams_list_channel_messages(
context: ActorContext,
) -> Result<HashMap<String, Message>, Error> {
let inputs = context.get_payload();
let actor_config = context.get_config();
let mut endpoint = "/teams/{team-id}/channels/{channel-id}/messages".to_string();
if let Some(val) = inputs.get("team_id") {
endpoint = endpoint.replace("{{team-id}}", &super::message_to_str(val));
}
if let Some(val) = inputs.get("channel_id") {
endpoint = endpoint.replace("{{channel-id}}", &super::message_to_str(val));
}
let url = format!("{}{}", BASE_URL.trim_end_matches('/'), endpoint);
let client = reqwest::Client::builder()
.timeout(Duration::from_secs(30))
.build()?;
let mut builder = client.get(&url);
builder = builder.header("Content-Type", "application/json");
builder = apply_auth(actor_config, builder)?;
let mut query_pairs: Vec<(&str, String)> = Vec::new();
if let Some(val) = inputs.get("top") {
query_pairs.push(("$top", super::message_to_str(val)));
}
if let Some(val) = inputs.get("skip") {
query_pairs.push(("$skip", super::message_to_str(val)));
}
if let Some(val) = inputs.get("filter") {
query_pairs.push(("$filter", super::message_to_str(val)));
}
if !query_pairs.is_empty() {
builder = builder.query(&query_pairs);
}
let mut output = HashMap::new();
match builder.send().await {
Ok(resp) => {
let status = resp.status().as_u16();
let headers: HashMap<String, String> = resp
.headers()
.iter()
.filter_map(|(k, v)| v.to_str().ok().map(|val| (k.to_string(), val.to_string())))
.collect();
let body_text = resp.text().await.unwrap_or_default();
let body_value: Value =
serde_json::from_str(&body_text).unwrap_or(Value::String(body_text));
output.insert(
"response".to_string(),
Message::object(EncodableValue::from(json!({
"status": status,
"headers": headers,
"body": body_value,
}))),
);
}
Err(e) => {
output.insert(
"error".to_string(),
Message::Error(
format!(
"GET /teams/{{team-id}}/channels/{{channel-id}}/messages failed: {}",
e
)
.into(),
),
);
}
}
Ok(output)
}
#[actor(
MicrosoftTeamsSendChannelMessageActor,
inports::<100>(team_id, channel_id, body, subject, attachments, mentions, importance),
outports::<50>(response, error),
state(MemoryState)
)]
pub async fn microsoft_teams_send_channel_message(
context: ActorContext,
) -> Result<HashMap<String, Message>, Error> {
let inputs = context.get_payload();
let actor_config = context.get_config();
let mut endpoint = "/teams/{team-id}/channels/{channel-id}/messages".to_string();
if let Some(val) = inputs.get("team_id") {
endpoint = endpoint.replace("{{team-id}}", &super::message_to_str(val));
}
if let Some(val) = inputs.get("channel_id") {
endpoint = endpoint.replace("{{channel-id}}", &super::message_to_str(val));
}
let url = format!("{}{}", BASE_URL.trim_end_matches('/'), endpoint);
let client = reqwest::Client::builder()
.timeout(Duration::from_secs(30))
.build()?;
let mut builder = client.post(&url);
builder = builder.header("Content-Type", "application/json");
builder = apply_auth(actor_config, builder)?;
let mut body = serde_json::Map::new();
if let Some(val) = inputs.get("body") {
body.insert("body".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("subject") {
body.insert("subject".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("attachments") {
body.insert("attachments".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("mentions") {
body.insert("mentions".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("importance") {
body.insert("importance".to_string(), val.clone().into());
}
if !body.is_empty() {
builder = builder.json(&serde_json::Value::Object(body));
}
let mut output = HashMap::new();
match builder.send().await {
Ok(resp) => {
let status = resp.status().as_u16();
let headers: HashMap<String, String> = resp
.headers()
.iter()
.filter_map(|(k, v)| v.to_str().ok().map(|val| (k.to_string(), val.to_string())))
.collect();
let body_text = resp.text().await.unwrap_or_default();
let body_value: Value =
serde_json::from_str(&body_text).unwrap_or(Value::String(body_text));
output.insert(
"response".to_string(),
Message::object(EncodableValue::from(json!({
"status": status,
"headers": headers,
"body": body_value,
}))),
);
}
Err(e) => {
output.insert(
"error".to_string(),
Message::Error(
format!(
"POST /teams/{{team-id}}/channels/{{channel-id}}/messages failed: {}",
e
)
.into(),
),
);
}
}
Ok(output)
}
#[actor(
MicrosoftTeamsReadChannelMessageActor,
inports::<100>(team_id, channel_id, message_id),
outports::<50>(response, error),
state(MemoryState)
)]
pub async fn microsoft_teams_read_channel_message(
context: ActorContext,
) -> Result<HashMap<String, Message>, Error> {
let inputs = context.get_payload();
let actor_config = context.get_config();
let mut endpoint = "/teams/{team-id}/channels/{channel-id}/messages/{message-id}".to_string();
if let Some(val) = inputs.get("team_id") {
endpoint = endpoint.replace("{{team-id}}", &super::message_to_str(val));
}
if let Some(val) = inputs.get("channel_id") {
endpoint = endpoint.replace("{{channel-id}}", &super::message_to_str(val));
}
if let Some(val) = inputs.get("message_id") {
endpoint = endpoint.replace("{{message-id}}", &super::message_to_str(val));
}
let url = format!("{}{}", BASE_URL.trim_end_matches('/'), endpoint);
let client = reqwest::Client::builder()
.timeout(Duration::from_secs(30))
.build()?;
let mut builder = client.get(&url);
builder = builder.header("Content-Type", "application/json");
builder = apply_auth(actor_config, builder)?;
let mut output = HashMap::new();
match builder.send().await {
Ok(resp) => {
let status = resp.status().as_u16();
let headers: HashMap<String, String> = resp
.headers()
.iter()
.filter_map(|(k, v)| v.to_str().ok().map(|val| (k.to_string(), val.to_string())))
.collect();
let body_text = resp.text().await.unwrap_or_default();
let body_value: Value =
serde_json::from_str(&body_text).unwrap_or(Value::String(body_text));
output.insert(
"response".to_string(),
Message::object(EncodableValue::from(json!({
"status": status,
"headers": headers,
"body": body_value,
}))),
);
}
Err(e) => {
output.insert("error".to_string(), Message::Error(format!("GET /teams/{{team-id}}/channels/{{channel-id}}/messages/{{message-id}} failed: {}", e).into()));
}
}
Ok(output)
}
#[actor(
MicrosoftTeamsSendMessageReplyActor,
inports::<100>(team_id, channel_id, message_id, body, attachments, mentions),
outports::<50>(response, error),
state(MemoryState)
)]
pub async fn microsoft_teams_send_message_reply(
context: ActorContext,
) -> Result<HashMap<String, Message>, Error> {
let inputs = context.get_payload();
let actor_config = context.get_config();
let mut endpoint =
"/teams/{team-id}/channels/{channel-id}/messages/{message-id}/replies".to_string();
if let Some(val) = inputs.get("team_id") {
endpoint = endpoint.replace("{{team-id}}", &super::message_to_str(val));
}
if let Some(val) = inputs.get("channel_id") {
endpoint = endpoint.replace("{{channel-id}}", &super::message_to_str(val));
}
if let Some(val) = inputs.get("message_id") {
endpoint = endpoint.replace("{{message-id}}", &super::message_to_str(val));
}
let url = format!("{}{}", BASE_URL.trim_end_matches('/'), endpoint);
let client = reqwest::Client::builder()
.timeout(Duration::from_secs(30))
.build()?;
let mut builder = client.post(&url);
builder = builder.header("Content-Type", "application/json");
builder = apply_auth(actor_config, builder)?;
let mut body = serde_json::Map::new();
if let Some(val) = inputs.get("body") {
body.insert("body".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("attachments") {
body.insert("attachments".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("mentions") {
body.insert("mentions".to_string(), val.clone().into());
}
if !body.is_empty() {
builder = builder.json(&serde_json::Value::Object(body));
}
let mut output = HashMap::new();
match builder.send().await {
Ok(resp) => {
let status = resp.status().as_u16();
let headers: HashMap<String, String> = resp
.headers()
.iter()
.filter_map(|(k, v)| v.to_str().ok().map(|val| (k.to_string(), val.to_string())))
.collect();
let body_text = resp.text().await.unwrap_or_default();
let body_value: Value =
serde_json::from_str(&body_text).unwrap_or(Value::String(body_text));
output.insert(
"response".to_string(),
Message::object(EncodableValue::from(json!({
"status": status,
"headers": headers,
"body": body_value,
}))),
);
}
Err(e) => {
output.insert("error".to_string(), Message::Error(format!("POST /teams/{{team-id}}/channels/{{channel-id}}/messages/{{message-id}}/replies failed: {}", e).into()));
}
}
Ok(output)
}
#[actor(
MicrosoftTeamsListMessageRepliesActor,
inports::<100>(team_id, channel_id, message_id, top),
outports::<50>(response, error),
state(MemoryState)
)]
pub async fn microsoft_teams_list_message_replies(
context: ActorContext,
) -> Result<HashMap<String, Message>, Error> {
let inputs = context.get_payload();
let actor_config = context.get_config();
let mut endpoint =
"/teams/{team-id}/channels/{channel-id}/messages/{message-id}/replies".to_string();
if let Some(val) = inputs.get("team_id") {
endpoint = endpoint.replace("{{team-id}}", &super::message_to_str(val));
}
if let Some(val) = inputs.get("channel_id") {
endpoint = endpoint.replace("{{channel-id}}", &super::message_to_str(val));
}
if let Some(val) = inputs.get("message_id") {
endpoint = endpoint.replace("{{message-id}}", &super::message_to_str(val));
}
let url = format!("{}{}", BASE_URL.trim_end_matches('/'), endpoint);
let client = reqwest::Client::builder()
.timeout(Duration::from_secs(30))
.build()?;
let mut builder = client.get(&url);
builder = builder.header("Content-Type", "application/json");
builder = apply_auth(actor_config, builder)?;
let mut query_pairs: Vec<(&str, String)> = Vec::new();
if let Some(val) = inputs.get("top") {
query_pairs.push(("$top", super::message_to_str(val)));
}
if !query_pairs.is_empty() {
builder = builder.query(&query_pairs);
}
let mut output = HashMap::new();
match builder.send().await {
Ok(resp) => {
let status = resp.status().as_u16();
let headers: HashMap<String, String> = resp
.headers()
.iter()
.filter_map(|(k, v)| v.to_str().ok().map(|val| (k.to_string(), val.to_string())))
.collect();
let body_text = resp.text().await.unwrap_or_default();
let body_value: Value =
serde_json::from_str(&body_text).unwrap_or(Value::String(body_text));
output.insert(
"response".to_string(),
Message::object(EncodableValue::from(json!({
"status": status,
"headers": headers,
"body": body_value,
}))),
);
}
Err(e) => {
output.insert("error".to_string(), Message::Error(format!("GET /teams/{{team-id}}/channels/{{channel-id}}/messages/{{message-id}}/replies failed: {}", e).into()));
}
}
Ok(output)
}
#[actor(
MicrosoftTeamsListTeamMembersActor,
inports::<100>(team_id, filter, select),
outports::<50>(response, error),
state(MemoryState)
)]
pub async fn microsoft_teams_list_team_members(
context: ActorContext,
) -> Result<HashMap<String, Message>, Error> {
let inputs = context.get_payload();
let actor_config = context.get_config();
let mut endpoint = "/teams/{team-id}/members".to_string();
if let Some(val) = inputs.get("team_id") {
endpoint = endpoint.replace("{{team-id}}", &super::message_to_str(val));
}
let url = format!("{}{}", BASE_URL.trim_end_matches('/'), endpoint);
let client = reqwest::Client::builder()
.timeout(Duration::from_secs(30))
.build()?;
let mut builder = client.get(&url);
builder = builder.header("Content-Type", "application/json");
builder = apply_auth(actor_config, builder)?;
let mut query_pairs: Vec<(&str, String)> = Vec::new();
if let Some(val) = inputs.get("filter") {
query_pairs.push(("$filter", super::message_to_str(val)));
}
if let Some(val) = inputs.get("select") {
query_pairs.push(("$select", super::message_to_str(val)));
}
if !query_pairs.is_empty() {
builder = builder.query(&query_pairs);
}
let mut output = HashMap::new();
match builder.send().await {
Ok(resp) => {
let status = resp.status().as_u16();
let headers: HashMap<String, String> = resp
.headers()
.iter()
.filter_map(|(k, v)| v.to_str().ok().map(|val| (k.to_string(), val.to_string())))
.collect();
let body_text = resp.text().await.unwrap_or_default();
let body_value: Value =
serde_json::from_str(&body_text).unwrap_or(Value::String(body_text));
output.insert(
"response".to_string(),
Message::object(EncodableValue::from(json!({
"status": status,
"headers": headers,
"body": body_value,
}))),
);
}
Err(e) => {
output.insert(
"error".to_string(),
Message::Error(format!("GET /teams/{{team-id}}/members failed: {}", e).into()),
);
}
}
Ok(output)
}
#[actor(
MicrosoftTeamsCreateTeamMemberActor,
inports::<100>(team_id, odata_type, roles, user_odata_bind),
outports::<50>(response, error),
state(MemoryState)
)]
pub async fn microsoft_teams_create_team_member(
context: ActorContext,
) -> Result<HashMap<String, Message>, Error> {
let inputs = context.get_payload();
let actor_config = context.get_config();
let mut endpoint = "/teams/{team-id}/members".to_string();
if let Some(val) = inputs.get("team_id") {
endpoint = endpoint.replace("{{team-id}}", &super::message_to_str(val));
}
let url = format!("{}{}", BASE_URL.trim_end_matches('/'), endpoint);
let client = reqwest::Client::builder()
.timeout(Duration::from_secs(30))
.build()?;
let mut builder = client.post(&url);
builder = builder.header("Content-Type", "application/json");
builder = apply_auth(actor_config, builder)?;
let mut body = serde_json::Map::new();
if let Some(val) = inputs.get("odata_type") {
body.insert("@odata.type".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("roles") {
body.insert("roles".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("user_odata_bind") {
body.insert("user@odata.bind".to_string(), val.clone().into());
}
if !body.is_empty() {
builder = builder.json(&serde_json::Value::Object(body));
}
let mut output = HashMap::new();
match builder.send().await {
Ok(resp) => {
let status = resp.status().as_u16();
let headers: HashMap<String, String> = resp
.headers()
.iter()
.filter_map(|(k, v)| v.to_str().ok().map(|val| (k.to_string(), val.to_string())))
.collect();
let body_text = resp.text().await.unwrap_or_default();
let body_value: Value =
serde_json::from_str(&body_text).unwrap_or(Value::String(body_text));
output.insert(
"response".to_string(),
Message::object(EncodableValue::from(json!({
"status": status,
"headers": headers,
"body": body_value,
}))),
);
}
Err(e) => {
output.insert(
"error".to_string(),
Message::Error(format!("POST /teams/{{team-id}}/members failed: {}", e).into()),
);
}
}
Ok(output)
}
#[actor(
MicrosoftTeamsDeleteTeamMemberActor,
inports::<100>(team_id, membership_id),
outports::<50>(response, error),
state(MemoryState)
)]
pub async fn microsoft_teams_delete_team_member(
context: ActorContext,
) -> Result<HashMap<String, Message>, Error> {
let inputs = context.get_payload();
let actor_config = context.get_config();
let mut endpoint = "/teams/{team-id}/members/{membership-id}".to_string();
if let Some(val) = inputs.get("team_id") {
endpoint = endpoint.replace("{{team-id}}", &super::message_to_str(val));
}
if let Some(val) = inputs.get("membership_id") {
endpoint = endpoint.replace("{{membership-id}}", &super::message_to_str(val));
}
let url = format!("{}{}", BASE_URL.trim_end_matches('/'), endpoint);
let client = reqwest::Client::builder()
.timeout(Duration::from_secs(30))
.build()?;
let mut builder = client.delete(&url);
builder = builder.header("Content-Type", "application/json");
builder = apply_auth(actor_config, builder)?;
let mut output = HashMap::new();
match builder.send().await {
Ok(resp) => {
let status = resp.status().as_u16();
let headers: HashMap<String, String> = resp
.headers()
.iter()
.filter_map(|(k, v)| v.to_str().ok().map(|val| (k.to_string(), val.to_string())))
.collect();
let body_text = resp.text().await.unwrap_or_default();
let body_value: Value =
serde_json::from_str(&body_text).unwrap_or(Value::String(body_text));
output.insert(
"response".to_string(),
Message::object(EncodableValue::from(json!({
"status": status,
"headers": headers,
"body": body_value,
}))),
);
}
Err(e) => {
output.insert(
"error".to_string(),
Message::Error(
format!(
"DELETE /teams/{{team-id}}/members/{{membership-id}} failed: {}",
e
)
.into(),
),
);
}
}
Ok(output)
}
#[actor(
MicrosoftTeamsListChatsActor,
inports::<100>(top, filter, expand),
outports::<50>(response, error),
state(MemoryState)
)]
pub async fn microsoft_teams_list_chats(
context: ActorContext,
) -> Result<HashMap<String, Message>, Error> {
let inputs = context.get_payload();
let actor_config = context.get_config();
let endpoint = "/me/chats".to_string();
let url = format!("{}{}", BASE_URL.trim_end_matches('/'), endpoint);
let client = reqwest::Client::builder()
.timeout(Duration::from_secs(30))
.build()?;
let mut builder = client.get(&url);
builder = builder.header("Content-Type", "application/json");
builder = apply_auth(actor_config, builder)?;
let mut query_pairs: Vec<(&str, String)> = Vec::new();
if let Some(val) = inputs.get("top") {
query_pairs.push(("$top", super::message_to_str(val)));
}
if let Some(val) = inputs.get("filter") {
query_pairs.push(("$filter", super::message_to_str(val)));
}
if let Some(val) = inputs.get("expand") {
query_pairs.push(("$expand", super::message_to_str(val)));
}
if !query_pairs.is_empty() {
builder = builder.query(&query_pairs);
}
let mut output = HashMap::new();
match builder.send().await {
Ok(resp) => {
let status = resp.status().as_u16();
let headers: HashMap<String, String> = resp
.headers()
.iter()
.filter_map(|(k, v)| v.to_str().ok().map(|val| (k.to_string(), val.to_string())))
.collect();
let body_text = resp.text().await.unwrap_or_default();
let body_value: Value =
serde_json::from_str(&body_text).unwrap_or(Value::String(body_text));
output.insert(
"response".to_string(),
Message::object(EncodableValue::from(json!({
"status": status,
"headers": headers,
"body": body_value,
}))),
);
}
Err(e) => {
output.insert(
"error".to_string(),
Message::Error(format!("GET /me/chats failed: {}", e).into()),
);
}
}
Ok(output)
}
#[actor(
MicrosoftTeamsCreateChatActor,
inports::<100>(chatType, members, topic),
outports::<50>(response, error),
state(MemoryState)
)]
pub async fn microsoft_teams_create_chat(
context: ActorContext,
) -> Result<HashMap<String, Message>, Error> {
let inputs = context.get_payload();
let actor_config = context.get_config();
let endpoint = "/chats".to_string();
let url = format!("{}{}", BASE_URL.trim_end_matches('/'), endpoint);
let client = reqwest::Client::builder()
.timeout(Duration::from_secs(30))
.build()?;
let mut builder = client.post(&url);
builder = builder.header("Content-Type", "application/json");
builder = apply_auth(actor_config, builder)?;
let mut body = serde_json::Map::new();
if let Some(val) = inputs.get("chatType") {
body.insert("chatType".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("members") {
body.insert("members".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("topic") {
body.insert("topic".to_string(), val.clone().into());
}
if !body.is_empty() {
builder = builder.json(&serde_json::Value::Object(body));
}
let mut output = HashMap::new();
match builder.send().await {
Ok(resp) => {
let status = resp.status().as_u16();
let headers: HashMap<String, String> = resp
.headers()
.iter()
.filter_map(|(k, v)| v.to_str().ok().map(|val| (k.to_string(), val.to_string())))
.collect();
let body_text = resp.text().await.unwrap_or_default();
let body_value: Value =
serde_json::from_str(&body_text).unwrap_or(Value::String(body_text));
output.insert(
"response".to_string(),
Message::object(EncodableValue::from(json!({
"status": status,
"headers": headers,
"body": body_value,
}))),
);
}
Err(e) => {
output.insert(
"error".to_string(),
Message::Error(format!("POST /chats failed: {}", e).into()),
);
}
}
Ok(output)
}
#[actor(
MicrosoftTeamsReadChatActor,
inports::<100>(chat_id, expand),
outports::<50>(response, error),
state(MemoryState)
)]
pub async fn microsoft_teams_read_chat(
context: ActorContext,
) -> Result<HashMap<String, Message>, Error> {
let inputs = context.get_payload();
let actor_config = context.get_config();
let mut endpoint = "/chats/{chat-id}".to_string();
if let Some(val) = inputs.get("chat_id") {
endpoint = endpoint.replace("{{chat-id}}", &super::message_to_str(val));
}
let url = format!("{}{}", BASE_URL.trim_end_matches('/'), endpoint);
let client = reqwest::Client::builder()
.timeout(Duration::from_secs(30))
.build()?;
let mut builder = client.get(&url);
builder = builder.header("Content-Type", "application/json");
builder = apply_auth(actor_config, builder)?;
let mut query_pairs: Vec<(&str, String)> = Vec::new();
if let Some(val) = inputs.get("expand") {
query_pairs.push(("$expand", super::message_to_str(val)));
}
if !query_pairs.is_empty() {
builder = builder.query(&query_pairs);
}
let mut output = HashMap::new();
match builder.send().await {
Ok(resp) => {
let status = resp.status().as_u16();
let headers: HashMap<String, String> = resp
.headers()
.iter()
.filter_map(|(k, v)| v.to_str().ok().map(|val| (k.to_string(), val.to_string())))
.collect();
let body_text = resp.text().await.unwrap_or_default();
let body_value: Value =
serde_json::from_str(&body_text).unwrap_or(Value::String(body_text));
output.insert(
"response".to_string(),
Message::object(EncodableValue::from(json!({
"status": status,
"headers": headers,
"body": body_value,
}))),
);
}
Err(e) => {
output.insert(
"error".to_string(),
Message::Error(format!("GET /chats/{{chat-id}} failed: {}", e).into()),
);
}
}
Ok(output)
}
#[actor(
MicrosoftTeamsListChatMessagesActor,
inports::<100>(chat_id, top, filter),
outports::<50>(response, error),
state(MemoryState)
)]
pub async fn microsoft_teams_list_chat_messages(
context: ActorContext,
) -> Result<HashMap<String, Message>, Error> {
let inputs = context.get_payload();
let actor_config = context.get_config();
let mut endpoint = "/chats/{chat-id}/messages".to_string();
if let Some(val) = inputs.get("chat_id") {
endpoint = endpoint.replace("{{chat-id}}", &super::message_to_str(val));
}
let url = format!("{}{}", BASE_URL.trim_end_matches('/'), endpoint);
let client = reqwest::Client::builder()
.timeout(Duration::from_secs(30))
.build()?;
let mut builder = client.get(&url);
builder = builder.header("Content-Type", "application/json");
builder = apply_auth(actor_config, builder)?;
let mut query_pairs: Vec<(&str, String)> = Vec::new();
if let Some(val) = inputs.get("top") {
query_pairs.push(("$top", super::message_to_str(val)));
}
if let Some(val) = inputs.get("filter") {
query_pairs.push(("$filter", super::message_to_str(val)));
}
if !query_pairs.is_empty() {
builder = builder.query(&query_pairs);
}
let mut output = HashMap::new();
match builder.send().await {
Ok(resp) => {
let status = resp.status().as_u16();
let headers: HashMap<String, String> = resp
.headers()
.iter()
.filter_map(|(k, v)| v.to_str().ok().map(|val| (k.to_string(), val.to_string())))
.collect();
let body_text = resp.text().await.unwrap_or_default();
let body_value: Value =
serde_json::from_str(&body_text).unwrap_or(Value::String(body_text));
output.insert(
"response".to_string(),
Message::object(EncodableValue::from(json!({
"status": status,
"headers": headers,
"body": body_value,
}))),
);
}
Err(e) => {
output.insert(
"error".to_string(),
Message::Error(format!("GET /chats/{{chat-id}}/messages failed: {}", e).into()),
);
}
}
Ok(output)
}
#[actor(
MicrosoftTeamsSendChatMessageActor,
inports::<100>(chat_id, body, attachments, mentions, importance),
outports::<50>(response, error),
state(MemoryState)
)]
pub async fn microsoft_teams_send_chat_message(
context: ActorContext,
) -> Result<HashMap<String, Message>, Error> {
let inputs = context.get_payload();
let actor_config = context.get_config();
let mut endpoint = "/chats/{chat-id}/messages".to_string();
if let Some(val) = inputs.get("chat_id") {
endpoint = endpoint.replace("{{chat-id}}", &super::message_to_str(val));
}
let url = format!("{}{}", BASE_URL.trim_end_matches('/'), endpoint);
let client = reqwest::Client::builder()
.timeout(Duration::from_secs(30))
.build()?;
let mut builder = client.post(&url);
builder = builder.header("Content-Type", "application/json");
builder = apply_auth(actor_config, builder)?;
let mut body = serde_json::Map::new();
if let Some(val) = inputs.get("body") {
body.insert("body".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("attachments") {
body.insert("attachments".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("mentions") {
body.insert("mentions".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("importance") {
body.insert("importance".to_string(), val.clone().into());
}
if !body.is_empty() {
builder = builder.json(&serde_json::Value::Object(body));
}
let mut output = HashMap::new();
match builder.send().await {
Ok(resp) => {
let status = resp.status().as_u16();
let headers: HashMap<String, String> = resp
.headers()
.iter()
.filter_map(|(k, v)| v.to_str().ok().map(|val| (k.to_string(), val.to_string())))
.collect();
let body_text = resp.text().await.unwrap_or_default();
let body_value: Value =
serde_json::from_str(&body_text).unwrap_or(Value::String(body_text));
output.insert(
"response".to_string(),
Message::object(EncodableValue::from(json!({
"status": status,
"headers": headers,
"body": body_value,
}))),
);
}
Err(e) => {
output.insert(
"error".to_string(),
Message::Error(format!("POST /chats/{{chat-id}}/messages failed: {}", e).into()),
);
}
}
Ok(output)
}
#[actor(
MicrosoftTeamsListOnlineMeetingsActor,
inports::<100>(filter, top),
outports::<50>(response, error),
state(MemoryState)
)]
pub async fn microsoft_teams_list_online_meetings(
context: ActorContext,
) -> Result<HashMap<String, Message>, Error> {
let inputs = context.get_payload();
let actor_config = context.get_config();
let endpoint = "/me/onlineMeetings".to_string();
let url = format!("{}{}", BASE_URL.trim_end_matches('/'), endpoint);
let client = reqwest::Client::builder()
.timeout(Duration::from_secs(30))
.build()?;
let mut builder = client.get(&url);
builder = builder.header("Content-Type", "application/json");
builder = apply_auth(actor_config, builder)?;
let mut query_pairs: Vec<(&str, String)> = Vec::new();
if let Some(val) = inputs.get("filter") {
query_pairs.push(("$filter", super::message_to_str(val)));
}
if let Some(val) = inputs.get("top") {
query_pairs.push(("$top", super::message_to_str(val)));
}
if !query_pairs.is_empty() {
builder = builder.query(&query_pairs);
}
let mut output = HashMap::new();
match builder.send().await {
Ok(resp) => {
let status = resp.status().as_u16();
let headers: HashMap<String, String> = resp
.headers()
.iter()
.filter_map(|(k, v)| v.to_str().ok().map(|val| (k.to_string(), val.to_string())))
.collect();
let body_text = resp.text().await.unwrap_or_default();
let body_value: Value =
serde_json::from_str(&body_text).unwrap_or(Value::String(body_text));
output.insert(
"response".to_string(),
Message::object(EncodableValue::from(json!({
"status": status,
"headers": headers,
"body": body_value,
}))),
);
}
Err(e) => {
output.insert(
"error".to_string(),
Message::Error(format!("GET /me/onlineMeetings failed: {}", e).into()),
);
}
}
Ok(output)
}
#[actor(
MicrosoftTeamsCreateOnlineMeetingActor,
inports::<100>(subject, startDateTime, endDateTime, participants, isEntryExitAnnounced, allowedPresenters, lobbyBypassSettings),
outports::<50>(response, error),
state(MemoryState)
)]
pub async fn microsoft_teams_create_online_meeting(
context: ActorContext,
) -> Result<HashMap<String, Message>, Error> {
let inputs = context.get_payload();
let actor_config = context.get_config();
let endpoint = "/me/onlineMeetings".to_string();
let url = format!("{}{}", BASE_URL.trim_end_matches('/'), endpoint);
let client = reqwest::Client::builder()
.timeout(Duration::from_secs(30))
.build()?;
let mut builder = client.post(&url);
builder = builder.header("Content-Type", "application/json");
builder = apply_auth(actor_config, builder)?;
let mut body = serde_json::Map::new();
if let Some(val) = inputs.get("subject") {
body.insert("subject".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("startDateTime") {
body.insert("startDateTime".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("endDateTime") {
body.insert("endDateTime".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("participants") {
body.insert("participants".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("isEntryExitAnnounced") {
body.insert("isEntryExitAnnounced".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("allowedPresenters") {
body.insert("allowedPresenters".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("lobbyBypassSettings") {
body.insert("lobbyBypassSettings".to_string(), val.clone().into());
}
if !body.is_empty() {
builder = builder.json(&serde_json::Value::Object(body));
}
let mut output = HashMap::new();
match builder.send().await {
Ok(resp) => {
let status = resp.status().as_u16();
let headers: HashMap<String, String> = resp
.headers()
.iter()
.filter_map(|(k, v)| v.to_str().ok().map(|val| (k.to_string(), val.to_string())))
.collect();
let body_text = resp.text().await.unwrap_or_default();
let body_value: Value =
serde_json::from_str(&body_text).unwrap_or(Value::String(body_text));
output.insert(
"response".to_string(),
Message::object(EncodableValue::from(json!({
"status": status,
"headers": headers,
"body": body_value,
}))),
);
}
Err(e) => {
output.insert(
"error".to_string(),
Message::Error(format!("POST /me/onlineMeetings failed: {}", e).into()),
);
}
}
Ok(output)
}
#[actor(
MicrosoftTeamsReadOnlineMeetingActor,
inports::<100>(meeting_id),
outports::<50>(response, error),
state(MemoryState)
)]
pub async fn microsoft_teams_read_online_meeting(
context: ActorContext,
) -> Result<HashMap<String, Message>, Error> {
let inputs = context.get_payload();
let actor_config = context.get_config();
let mut endpoint = "/me/onlineMeetings/{meeting-id}".to_string();
if let Some(val) = inputs.get("meeting_id") {
endpoint = endpoint.replace("{{meeting-id}}", &super::message_to_str(val));
}
let url = format!("{}{}", BASE_URL.trim_end_matches('/'), endpoint);
let client = reqwest::Client::builder()
.timeout(Duration::from_secs(30))
.build()?;
let mut builder = client.get(&url);
builder = builder.header("Content-Type", "application/json");
builder = apply_auth(actor_config, builder)?;
let mut output = HashMap::new();
match builder.send().await {
Ok(resp) => {
let status = resp.status().as_u16();
let headers: HashMap<String, String> = resp
.headers()
.iter()
.filter_map(|(k, v)| v.to_str().ok().map(|val| (k.to_string(), val.to_string())))
.collect();
let body_text = resp.text().await.unwrap_or_default();
let body_value: Value =
serde_json::from_str(&body_text).unwrap_or(Value::String(body_text));
output.insert(
"response".to_string(),
Message::object(EncodableValue::from(json!({
"status": status,
"headers": headers,
"body": body_value,
}))),
);
}
Err(e) => {
output.insert(
"error".to_string(),
Message::Error(
format!("GET /me/onlineMeetings/{{meeting-id}} failed: {}", e).into(),
),
);
}
}
Ok(output)
}
#[actor(
MicrosoftTeamsUpdateOnlineMeetingActor,
inports::<100>(meeting_id, subject, startDateTime, endDateTime),
outports::<50>(response, error),
state(MemoryState)
)]
pub async fn microsoft_teams_update_online_meeting(
context: ActorContext,
) -> Result<HashMap<String, Message>, Error> {
let inputs = context.get_payload();
let actor_config = context.get_config();
let mut endpoint = "/me/onlineMeetings/{meeting-id}".to_string();
if let Some(val) = inputs.get("meeting_id") {
endpoint = endpoint.replace("{{meeting-id}}", &super::message_to_str(val));
}
let url = format!("{}{}", BASE_URL.trim_end_matches('/'), endpoint);
let client = reqwest::Client::builder()
.timeout(Duration::from_secs(30))
.build()?;
let mut builder = client.patch(&url);
builder = builder.header("Content-Type", "application/json");
builder = apply_auth(actor_config, builder)?;
let mut body = serde_json::Map::new();
if let Some(val) = inputs.get("subject") {
body.insert("subject".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("startDateTime") {
body.insert("startDateTime".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("endDateTime") {
body.insert("endDateTime".to_string(), val.clone().into());
}
if !body.is_empty() {
builder = builder.json(&serde_json::Value::Object(body));
}
let mut output = HashMap::new();
match builder.send().await {
Ok(resp) => {
let status = resp.status().as_u16();
let headers: HashMap<String, String> = resp
.headers()
.iter()
.filter_map(|(k, v)| v.to_str().ok().map(|val| (k.to_string(), val.to_string())))
.collect();
let body_text = resp.text().await.unwrap_or_default();
let body_value: Value =
serde_json::from_str(&body_text).unwrap_or(Value::String(body_text));
output.insert(
"response".to_string(),
Message::object(EncodableValue::from(json!({
"status": status,
"headers": headers,
"body": body_value,
}))),
);
}
Err(e) => {
output.insert(
"error".to_string(),
Message::Error(
format!("PATCH /me/onlineMeetings/{{meeting-id}} failed: {}", e).into(),
),
);
}
}
Ok(output)
}
#[actor(
MicrosoftTeamsDeleteOnlineMeetingActor,
inports::<100>(meeting_id),
outports::<50>(response, error),
state(MemoryState)
)]
pub async fn microsoft_teams_delete_online_meeting(
context: ActorContext,
) -> Result<HashMap<String, Message>, Error> {
let inputs = context.get_payload();
let actor_config = context.get_config();
let mut endpoint = "/me/onlineMeetings/{meeting-id}".to_string();
if let Some(val) = inputs.get("meeting_id") {
endpoint = endpoint.replace("{{meeting-id}}", &super::message_to_str(val));
}
let url = format!("{}{}", BASE_URL.trim_end_matches('/'), endpoint);
let client = reqwest::Client::builder()
.timeout(Duration::from_secs(30))
.build()?;
let mut builder = client.delete(&url);
builder = builder.header("Content-Type", "application/json");
builder = apply_auth(actor_config, builder)?;
let mut output = HashMap::new();
match builder.send().await {
Ok(resp) => {
let status = resp.status().as_u16();
let headers: HashMap<String, String> = resp
.headers()
.iter()
.filter_map(|(k, v)| v.to_str().ok().map(|val| (k.to_string(), val.to_string())))
.collect();
let body_text = resp.text().await.unwrap_or_default();
let body_value: Value =
serde_json::from_str(&body_text).unwrap_or(Value::String(body_text));
output.insert(
"response".to_string(),
Message::object(EncodableValue::from(json!({
"status": status,
"headers": headers,
"body": body_value,
}))),
);
}
Err(e) => {
output.insert(
"error".to_string(),
Message::Error(
format!("DELETE /me/onlineMeetings/{{meeting-id}} failed: {}", e).into(),
),
);
}
}
Ok(output)
}
#[actor(
MicrosoftTeamsListTabsActor,
inports::<100>(team_id, channel_id, expand),
outports::<50>(response, error),
state(MemoryState)
)]
pub async fn microsoft_teams_list_tabs(
context: ActorContext,
) -> Result<HashMap<String, Message>, Error> {
let inputs = context.get_payload();
let actor_config = context.get_config();
let mut endpoint = "/teams/{team-id}/channels/{channel-id}/tabs".to_string();
if let Some(val) = inputs.get("team_id") {
endpoint = endpoint.replace("{{team-id}}", &super::message_to_str(val));
}
if let Some(val) = inputs.get("channel_id") {
endpoint = endpoint.replace("{{channel-id}}", &super::message_to_str(val));
}
let url = format!("{}{}", BASE_URL.trim_end_matches('/'), endpoint);
let client = reqwest::Client::builder()
.timeout(Duration::from_secs(30))
.build()?;
let mut builder = client.get(&url);
builder = builder.header("Content-Type", "application/json");
builder = apply_auth(actor_config, builder)?;
let mut query_pairs: Vec<(&str, String)> = Vec::new();
if let Some(val) = inputs.get("expand") {
query_pairs.push(("$expand", super::message_to_str(val)));
}
if !query_pairs.is_empty() {
builder = builder.query(&query_pairs);
}
let mut output = HashMap::new();
match builder.send().await {
Ok(resp) => {
let status = resp.status().as_u16();
let headers: HashMap<String, String> = resp
.headers()
.iter()
.filter_map(|(k, v)| v.to_str().ok().map(|val| (k.to_string(), val.to_string())))
.collect();
let body_text = resp.text().await.unwrap_or_default();
let body_value: Value =
serde_json::from_str(&body_text).unwrap_or(Value::String(body_text));
output.insert(
"response".to_string(),
Message::object(EncodableValue::from(json!({
"status": status,
"headers": headers,
"body": body_value,
}))),
);
}
Err(e) => {
output.insert(
"error".to_string(),
Message::Error(
format!(
"GET /teams/{{team-id}}/channels/{{channel-id}}/tabs failed: {}",
e
)
.into(),
),
);
}
}
Ok(output)
}
#[actor(
MicrosoftTeamsCreateTabActor,
inports::<100>(team_id, channel_id, displayName, teamsApp_odata_bind, configuration),
outports::<50>(response, error),
state(MemoryState)
)]
pub async fn microsoft_teams_create_tab(
context: ActorContext,
) -> Result<HashMap<String, Message>, Error> {
let inputs = context.get_payload();
let actor_config = context.get_config();
let mut endpoint = "/teams/{team-id}/channels/{channel-id}/tabs".to_string();
if let Some(val) = inputs.get("team_id") {
endpoint = endpoint.replace("{{team-id}}", &super::message_to_str(val));
}
if let Some(val) = inputs.get("channel_id") {
endpoint = endpoint.replace("{{channel-id}}", &super::message_to_str(val));
}
let url = format!("{}{}", BASE_URL.trim_end_matches('/'), endpoint);
let client = reqwest::Client::builder()
.timeout(Duration::from_secs(30))
.build()?;
let mut builder = client.post(&url);
builder = builder.header("Content-Type", "application/json");
builder = apply_auth(actor_config, builder)?;
let mut body = serde_json::Map::new();
if let Some(val) = inputs.get("displayName") {
body.insert("displayName".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("teamsApp_odata_bind") {
body.insert("teamsApp@odata.bind".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("configuration") {
body.insert("configuration".to_string(), val.clone().into());
}
if !body.is_empty() {
builder = builder.json(&serde_json::Value::Object(body));
}
let mut output = HashMap::new();
match builder.send().await {
Ok(resp) => {
let status = resp.status().as_u16();
let headers: HashMap<String, String> = resp
.headers()
.iter()
.filter_map(|(k, v)| v.to_str().ok().map(|val| (k.to_string(), val.to_string())))
.collect();
let body_text = resp.text().await.unwrap_or_default();
let body_value: Value =
serde_json::from_str(&body_text).unwrap_or(Value::String(body_text));
output.insert(
"response".to_string(),
Message::object(EncodableValue::from(json!({
"status": status,
"headers": headers,
"body": body_value,
}))),
);
}
Err(e) => {
output.insert(
"error".to_string(),
Message::Error(
format!(
"POST /teams/{{team-id}}/channels/{{channel-id}}/tabs failed: {}",
e
)
.into(),
),
);
}
}
Ok(output)
}
#[actor(
MicrosoftTeamsDeleteTabActor,
inports::<100>(team_id, channel_id, tab_id),
outports::<50>(response, error),
state(MemoryState)
)]
pub async fn microsoft_teams_delete_tab(
context: ActorContext,
) -> Result<HashMap<String, Message>, Error> {
let inputs = context.get_payload();
let actor_config = context.get_config();
let mut endpoint = "/teams/{team-id}/channels/{channel-id}/tabs/{tab-id}".to_string();
if let Some(val) = inputs.get("team_id") {
endpoint = endpoint.replace("{{team-id}}", &super::message_to_str(val));
}
if let Some(val) = inputs.get("channel_id") {
endpoint = endpoint.replace("{{channel-id}}", &super::message_to_str(val));
}
if let Some(val) = inputs.get("tab_id") {
endpoint = endpoint.replace("{{tab-id}}", &super::message_to_str(val));
}
let url = format!("{}{}", BASE_URL.trim_end_matches('/'), endpoint);
let client = reqwest::Client::builder()
.timeout(Duration::from_secs(30))
.build()?;
let mut builder = client.delete(&url);
builder = builder.header("Content-Type", "application/json");
builder = apply_auth(actor_config, builder)?;
let mut output = HashMap::new();
match builder.send().await {
Ok(resp) => {
let status = resp.status().as_u16();
let headers: HashMap<String, String> = resp
.headers()
.iter()
.filter_map(|(k, v)| v.to_str().ok().map(|val| (k.to_string(), val.to_string())))
.collect();
let body_text = resp.text().await.unwrap_or_default();
let body_value: Value =
serde_json::from_str(&body_text).unwrap_or(Value::String(body_text));
output.insert(
"response".to_string(),
Message::object(EncodableValue::from(json!({
"status": status,
"headers": headers,
"body": body_value,
}))),
);
}
Err(e) => {
output.insert("error".to_string(), Message::Error(format!("DELETE /teams/{{team-id}}/channels/{{channel-id}}/tabs/{{tab-id}} failed: {}", e).into()));
}
}
Ok(output)
}
#[actor(
MicrosoftTeamsListInstalledAppsActor,
inports::<100>(team_id, filter, expand),
outports::<50>(response, error),
state(MemoryState)
)]
pub async fn microsoft_teams_list_installed_apps(
context: ActorContext,
) -> Result<HashMap<String, Message>, Error> {
let inputs = context.get_payload();
let actor_config = context.get_config();
let mut endpoint = "/teams/{team-id}/installedApps".to_string();
if let Some(val) = inputs.get("team_id") {
endpoint = endpoint.replace("{{team-id}}", &super::message_to_str(val));
}
let url = format!("{}{}", BASE_URL.trim_end_matches('/'), endpoint);
let client = reqwest::Client::builder()
.timeout(Duration::from_secs(30))
.build()?;
let mut builder = client.get(&url);
builder = builder.header("Content-Type", "application/json");
builder = apply_auth(actor_config, builder)?;
let mut query_pairs: Vec<(&str, String)> = Vec::new();
if let Some(val) = inputs.get("filter") {
query_pairs.push(("$filter", super::message_to_str(val)));
}
if let Some(val) = inputs.get("expand") {
query_pairs.push(("$expand", super::message_to_str(val)));
}
if !query_pairs.is_empty() {
builder = builder.query(&query_pairs);
}
let mut output = HashMap::new();
match builder.send().await {
Ok(resp) => {
let status = resp.status().as_u16();
let headers: HashMap<String, String> = resp
.headers()
.iter()
.filter_map(|(k, v)| v.to_str().ok().map(|val| (k.to_string(), val.to_string())))
.collect();
let body_text = resp.text().await.unwrap_or_default();
let body_value: Value =
serde_json::from_str(&body_text).unwrap_or(Value::String(body_text));
output.insert(
"response".to_string(),
Message::object(EncodableValue::from(json!({
"status": status,
"headers": headers,
"body": body_value,
}))),
);
}
Err(e) => {
output.insert(
"error".to_string(),
Message::Error(
format!("GET /teams/{{team-id}}/installedApps failed: {}", e).into(),
),
);
}
}
Ok(output)
}
#[actor(
MicrosoftTeamsCreateInstalledAppActor,
inports::<100>(team_id, teamsApp_odata_bind),
outports::<50>(response, error),
state(MemoryState)
)]
pub async fn microsoft_teams_create_installed_app(
context: ActorContext,
) -> Result<HashMap<String, Message>, Error> {
let inputs = context.get_payload();
let actor_config = context.get_config();
let mut endpoint = "/teams/{team-id}/installedApps".to_string();
if let Some(val) = inputs.get("team_id") {
endpoint = endpoint.replace("{{team-id}}", &super::message_to_str(val));
}
let url = format!("{}{}", BASE_URL.trim_end_matches('/'), endpoint);
let client = reqwest::Client::builder()
.timeout(Duration::from_secs(30))
.build()?;
let mut builder = client.post(&url);
builder = builder.header("Content-Type", "application/json");
builder = apply_auth(actor_config, builder)?;
let mut body = serde_json::Map::new();
if let Some(val) = inputs.get("teamsApp_odata_bind") {
body.insert("teamsApp@odata.bind".to_string(), val.clone().into());
}
if !body.is_empty() {
builder = builder.json(&serde_json::Value::Object(body));
}
let mut output = HashMap::new();
match builder.send().await {
Ok(resp) => {
let status = resp.status().as_u16();
let headers: HashMap<String, String> = resp
.headers()
.iter()
.filter_map(|(k, v)| v.to_str().ok().map(|val| (k.to_string(), val.to_string())))
.collect();
let body_text = resp.text().await.unwrap_or_default();
let body_value: Value =
serde_json::from_str(&body_text).unwrap_or(Value::String(body_text));
output.insert(
"response".to_string(),
Message::object(EncodableValue::from(json!({
"status": status,
"headers": headers,
"body": body_value,
}))),
);
}
Err(e) => {
output.insert(
"error".to_string(),
Message::Error(
format!("POST /teams/{{team-id}}/installedApps failed: {}", e).into(),
),
);
}
}
Ok(output)
}
#[actor(
MicrosoftTeamsDeleteInstalledAppActor,
inports::<100>(team_id, app_installation_id),
outports::<50>(response, error),
state(MemoryState)
)]
pub async fn microsoft_teams_delete_installed_app(
context: ActorContext,
) -> Result<HashMap<String, Message>, Error> {
let inputs = context.get_payload();
let actor_config = context.get_config();
let mut endpoint = "/teams/{team-id}/installedApps/{app-installation-id}".to_string();
if let Some(val) = inputs.get("team_id") {
endpoint = endpoint.replace("{{team-id}}", &super::message_to_str(val));
}
if let Some(val) = inputs.get("app_installation_id") {
endpoint = endpoint.replace("{{app-installation-id}}", &super::message_to_str(val));
}
let url = format!("{}{}", BASE_URL.trim_end_matches('/'), endpoint);
let client = reqwest::Client::builder()
.timeout(Duration::from_secs(30))
.build()?;
let mut builder = client.delete(&url);
builder = builder.header("Content-Type", "application/json");
builder = apply_auth(actor_config, builder)?;
let mut output = HashMap::new();
match builder.send().await {
Ok(resp) => {
let status = resp.status().as_u16();
let headers: HashMap<String, String> = resp
.headers()
.iter()
.filter_map(|(k, v)| v.to_str().ok().map(|val| (k.to_string(), val.to_string())))
.collect();
let body_text = resp.text().await.unwrap_or_default();
let body_value: Value =
serde_json::from_str(&body_text).unwrap_or(Value::String(body_text));
output.insert(
"response".to_string(),
Message::object(EncodableValue::from(json!({
"status": status,
"headers": headers,
"body": body_value,
}))),
);
}
Err(e) => {
output.insert("error".to_string(), Message::Error(format!("DELETE /teams/{{team-id}}/installedApps/{{app-installation-id}} failed: {}", e).into()));
}
}
Ok(output)
}
#[actor(
MicrosoftTeamsListSchedulesActor,
inports::<100>(team_id),
outports::<50>(response, error),
state(MemoryState)
)]
pub async fn microsoft_teams_list_schedules(
context: ActorContext,
) -> Result<HashMap<String, Message>, Error> {
let inputs = context.get_payload();
let actor_config = context.get_config();
let mut endpoint = "/teams/{team-id}/schedule".to_string();
if let Some(val) = inputs.get("team_id") {
endpoint = endpoint.replace("{{team-id}}", &super::message_to_str(val));
}
let url = format!("{}{}", BASE_URL.trim_end_matches('/'), endpoint);
let client = reqwest::Client::builder()
.timeout(Duration::from_secs(30))
.build()?;
let mut builder = client.get(&url);
builder = builder.header("Content-Type", "application/json");
builder = apply_auth(actor_config, builder)?;
let mut output = HashMap::new();
match builder.send().await {
Ok(resp) => {
let status = resp.status().as_u16();
let headers: HashMap<String, String> = resp
.headers()
.iter()
.filter_map(|(k, v)| v.to_str().ok().map(|val| (k.to_string(), val.to_string())))
.collect();
let body_text = resp.text().await.unwrap_or_default();
let body_value: Value =
serde_json::from_str(&body_text).unwrap_or(Value::String(body_text));
output.insert(
"response".to_string(),
Message::object(EncodableValue::from(json!({
"status": status,
"headers": headers,
"body": body_value,
}))),
);
}
Err(e) => {
output.insert(
"error".to_string(),
Message::Error(format!("GET /teams/{{team-id}}/schedule failed: {}", e).into()),
);
}
}
Ok(output)
}
#[actor(
MicrosoftTeamsListShiftsActor,
inports::<100>(team_id, filter),
outports::<50>(response, error),
state(MemoryState)
)]
pub async fn microsoft_teams_list_shifts(
context: ActorContext,
) -> Result<HashMap<String, Message>, Error> {
let inputs = context.get_payload();
let actor_config = context.get_config();
let mut endpoint = "/teams/{team-id}/schedule/shifts".to_string();
if let Some(val) = inputs.get("team_id") {
endpoint = endpoint.replace("{{team-id}}", &super::message_to_str(val));
}
let url = format!("{}{}", BASE_URL.trim_end_matches('/'), endpoint);
let client = reqwest::Client::builder()
.timeout(Duration::from_secs(30))
.build()?;
let mut builder = client.get(&url);
builder = builder.header("Content-Type", "application/json");
builder = apply_auth(actor_config, builder)?;
let mut query_pairs: Vec<(&str, String)> = Vec::new();
if let Some(val) = inputs.get("filter") {
query_pairs.push(("$filter", super::message_to_str(val)));
}
if !query_pairs.is_empty() {
builder = builder.query(&query_pairs);
}
let mut output = HashMap::new();
match builder.send().await {
Ok(resp) => {
let status = resp.status().as_u16();
let headers: HashMap<String, String> = resp
.headers()
.iter()
.filter_map(|(k, v)| v.to_str().ok().map(|val| (k.to_string(), val.to_string())))
.collect();
let body_text = resp.text().await.unwrap_or_default();
let body_value: Value =
serde_json::from_str(&body_text).unwrap_or(Value::String(body_text));
output.insert(
"response".to_string(),
Message::object(EncodableValue::from(json!({
"status": status,
"headers": headers,
"body": body_value,
}))),
);
}
Err(e) => {
output.insert(
"error".to_string(),
Message::Error(
format!("GET /teams/{{team-id}}/schedule/shifts failed: {}", e).into(),
),
);
}
}
Ok(output)
}
#[actor(
MicrosoftTeamsCreateShiftActor,
inports::<100>(team_id, userId, schedulingGroupId, sharedShift, draftShift),
outports::<50>(response, error),
state(MemoryState)
)]
pub async fn microsoft_teams_create_shift(
context: ActorContext,
) -> Result<HashMap<String, Message>, Error> {
let inputs = context.get_payload();
let actor_config = context.get_config();
let mut endpoint = "/teams/{team-id}/schedule/shifts".to_string();
if let Some(val) = inputs.get("team_id") {
endpoint = endpoint.replace("{{team-id}}", &super::message_to_str(val));
}
let url = format!("{}{}", BASE_URL.trim_end_matches('/'), endpoint);
let client = reqwest::Client::builder()
.timeout(Duration::from_secs(30))
.build()?;
let mut builder = client.post(&url);
builder = builder.header("Content-Type", "application/json");
builder = apply_auth(actor_config, builder)?;
let mut body = serde_json::Map::new();
if let Some(val) = inputs.get("userId") {
body.insert("userId".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("schedulingGroupId") {
body.insert("schedulingGroupId".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("sharedShift") {
body.insert("sharedShift".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("draftShift") {
body.insert("draftShift".to_string(), val.clone().into());
}
if !body.is_empty() {
builder = builder.json(&serde_json::Value::Object(body));
}
let mut output = HashMap::new();
match builder.send().await {
Ok(resp) => {
let status = resp.status().as_u16();
let headers: HashMap<String, String> = resp
.headers()
.iter()
.filter_map(|(k, v)| v.to_str().ok().map(|val| (k.to_string(), val.to_string())))
.collect();
let body_text = resp.text().await.unwrap_or_default();
let body_value: Value =
serde_json::from_str(&body_text).unwrap_or(Value::String(body_text));
output.insert(
"response".to_string(),
Message::object(EncodableValue::from(json!({
"status": status,
"headers": headers,
"body": body_value,
}))),
);
}
Err(e) => {
output.insert(
"error".to_string(),
Message::Error(
format!("POST /teams/{{team-id}}/schedule/shifts failed: {}", e).into(),
),
);
}
}
Ok(output)
}