Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
SendTextMessage.cs
1using System;
3using System.Text;
4using System.Threading.Tasks;
5using System.Xml;
6using Waher.Content;
13using Waher.Script;
16
18{
23 {
28 : base("Xmpp/SendTextMessage",
29 new KeyValuePair<Type, Expression>(typeof(Dictionary<string, object>), new Expression(jsonPattern)),
30 new KeyValuePair<Type, Expression>(typeof(XmlDocument), new Expression(xmlPattern)))
31 {
32 }
33
34 private static readonly string jsonPattern = Resources.LoadResourceAsText(typeof(SendTextMessage).Namespace + ".JSON.SendTextMessage.req");
35 private static readonly string xmlPattern = Resources.LoadResourceAsText(typeof(SendTextMessage).Namespace + ".XML.SendTextMessage.req");
36
45 public override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary<string, IElement> Parameters)
46 {
48
49 string To = (string)Parameters["PTo"].AssociatedObjectValue;
50
51 if (string.IsNullOrEmpty(To))
52 throw new BadRequestException("Missing recipient JID.");
53
54 if (!IsValidJID(To))
55 throw new BadRequestException("Recpient must be a Bare or Full JID.");
56
57 string Message = (string)Parameters["PMessage"].AssociatedObjectValue;
58
59 if (string.IsNullOrEmpty(Message))
60 throw new BadRequestException("Message is empty.");
61
62 string Subject = (string)Parameters["PSubject"]?.AssociatedObjectValue;
63 string Language = (string)Parameters["PLanguage"]?.AssociatedObjectValue;
64 string Id = (string)Parameters["PId"]?.AssociatedObjectValue;
65
66 StringBuilder Xml = new StringBuilder();
67
68 if (!string.IsNullOrEmpty(Subject))
69 {
70 Xml.Append("<subject>");
71 Xml.Append(XML.Encode(Subject));
72 Xml.Append("</subject>");
73 }
74
75 Xml.Append("<body>");
76 Xml.Append(XML.Encode(Message));
77 Xml.Append("</body>");
78
79 if (string.IsNullOrEmpty(Id))
80 Id = Guid.NewGuid().ToString();
81
82 bool Result;
83
84 if (XmppServerModule.Server is null)
85 {
87
88 Result = false;
89
90 if (Client is null)
91 {
92 if (Types.TryGetModuleParameter("XMPP", out XmppClient Client2))
93 Client = Client2;
94 }
95
96 if (Client is null)
97 throw new ServiceUnavailableException("XMPP Client not available.");
98
99 await Client.SendMessage(QoSLevel.Unacknowledged, MessageType.Chat, Id, To, Xml.ToString(),
100 string.Empty, string.Empty, Language, string.Empty, string.Empty, null, null);
101
102 Result = true;
103 }
104 else
105 {
106 Result = await XmppServerModule.Server.SendMessage("chat", Id,
107 User.Account.UserName + "@" + XmppServerModule.Server.Domain, To,
108 Language, Xml.ToString());
109 }
110
111 await Response.Return(new NamedDictionary<string, object>("StanzaResult", AgentNamespace)
112 {
113 { "sent", Result },
114 { "id", Id }
115 });
116 }
117 }
118}
A Named dictionary is a dictionary, with a local name and a namespace. Use it to return content that ...
Helps with common XML-related tasks.
Definition: XML.cs:21
static string Encode(string s)
Encodes a string for use in XML.
Definition: XML.cs:29
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:147
static XmppClient XmppClient
XMPP Client connection of gateway.
Definition: Gateway.cs:4038
The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repe...
Represents an HTTP request.
Definition: HttpRequest.cs:22
Represets a response of an HTTP client request.
Definition: HttpResponse.cs:23
Task Return(Exception ex)
Returns an error to the client.
The server is currently unable to handle the request due to a temporary overloading or maintenance of...
Manages an XMPP client connection. Implements XMPP, as defined in https://tools.ietf....
Definition: XmppClient.cs:58
Task SendMessage(MessageType Type, string To, string CustomXml, string Body, string Subject, string Language, string ThreadId, string ParentThreadId)
Sends a simple chat message
Definition: XmppClient.cs:5447
Static class managing loading of resources stored as embedded resources or in content files.
Definition: Resources.cs:13
static string LoadResourceAsText(string ResourceName)
Loads a text resource from an embedded resource.
Definition: Resources.cs:55
Static class that dynamically manages types and interfaces available in the runtime environment.
Definition: Types.cs:15
static bool TryGetModuleParameter(string Name, out object Value)
Tries to get a module parameter value.
Definition: Types.cs:607
Class managing a script expression.
Definition: Expression.cs:41
CaseInsensitiveString UserName
User Name of account
Definition: Account.cs:141
static AccountUser AssertUserAuthenticated(HttpRequest Request)
Makes sure the request is made by an authenticated API user.
const string AgentNamespace
https://waher.se/Schema/BrokerAgent.xsd
Abstract base class for XMPP resources.
static bool IsValidJID(string JID)
Checks if a string is a valid Bare or Full JID.
SendTextMessage()
Sends a text message to an XMPP recipient.
override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary< string, IElement > Parameters)
Executes the POST method on the resource.
Service Module hosting the XMPP broker and its components.
QoSLevel
Quality of Service Level for asynchronous messages. Support for QoS Levels must be supported by the r...
Definition: QoSLevel.cs:8
MessageType
Type of message received.
Definition: MessageType.cs:7