Address.cs
1 //
2 // Copyright (c) Microsoft. All rights reserved.
3 // Licensed under the MIT license.
4 //
5 // Microsoft Bot Framework: http://botframework.com
6 //
7 // Bot Builder SDK GitHub:
8 // https://github.com/Microsoft/BotBuilder
9 //
10 // Copyright (c) Microsoft Corporation
11 // All rights reserved.
12 //
13 // MIT License:
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 //
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 //
25 // THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33 
35 using Microsoft.Bot.Connector;
36 using Newtonsoft.Json;
37 using System;
38 using System.Collections.Generic;
39 
40 namespace Microsoft.Bot.Builder.Dialogs
41 {
45  public interface IAddress
46  {
47  string BotId { get; }
48  string ChannelId { get; }
49  string UserId { get; }
50  string ConversationId { get; }
51  string ServiceUrl { get; }
52  }
53 
57  [Serializable]
58  public sealed class Address : IAddress, IEquatable<IAddress>
59  {
60  public static Address FromActivity(IActivity activity)
61  {
62  return new Address
63  (
64  // purposefully using named arguments because these all have the same type
65  botId: activity.Recipient.Id,
66  channelId: activity.ChannelId,
67  userId: activity.From.Id,
68  conversationId: activity.Conversation.Id,
69  serviceUrl: activity.ServiceUrl
70  );
71  }
72 
73  [JsonConstructor]
74  public Address(string botId, string channelId, string userId, string conversationId, string serviceUrl)
75  {
76  SetField.CheckNull(nameof(botId), botId);
77  SetField.CheckNull(nameof(channelId), channelId);
78  SetField.CheckNull(nameof(userId), userId);
79  SetField.CheckNull(nameof(conversationId), conversationId);
80  SetField.CheckNull(nameof(serviceUrl), serviceUrl);
81 
82  this.BotId = botId;
83  this.ChannelId = channelId;
84  this.UserId = userId;
85  this.ConversationId = conversationId;
86  this.ServiceUrl = serviceUrl;
87  }
88  public string BotId { get; }
89  public string ChannelId { get; }
90  public string UserId { get; }
91  public string ConversationId { get; }
92  public string ServiceUrl { get; }
93 
94  public bool Equals(IAddress other)
95  {
96  return other != null
97  && object.Equals(this.BotId, other.BotId)
98  && object.Equals(this.ChannelId, other.ChannelId)
99  && object.Equals(this.UserId, other.UserId)
100  && object.Equals(this.ConversationId, other.ConversationId)
101  && object.Equals(this.ServiceUrl, other.ServiceUrl)
102  ;
103  }
104 
105  public override bool Equals(object other)
106  {
107  return this.Equals(other as IAddress);
108  }
109 
110  public override int GetHashCode()
111  {
112  var code
113  = this.BotId.GetHashCode()
114  ^ this.ChannelId.GetHashCode()
115  ^ this.UserId.GetHashCode()
116  ^ this.ConversationId.GetHashCode()
117  ^ this.ServiceUrl.GetHashCode()
118  ;
119 
120  return code;
121  }
122  }
123 
134  public sealed class ConversationAddressComparer : IEqualityComparer<IAddress>
135  {
136  bool IEqualityComparer<IAddress>.Equals(IAddress one, IAddress two)
137  {
138  var equals =
139  object.ReferenceEquals(one, two)
140  || (
141  object.Equals(one.BotId, two.BotId)
142  && object.Equals(one.ChannelId, two.ChannelId)
143  && object.Equals(one.ConversationId, two.ConversationId)
144  && object.Equals(one.ServiceUrl, two.ServiceUrl)
145  );
146 
147  return equals;
148  }
149 
150  int IEqualityComparer<IAddress>.GetHashCode(IAddress address)
151  {
152  var code
153  = address.BotId.GetHashCode()
154  ^ address.ChannelId.GetHashCode()
155  ^ address.ConversationId.GetHashCode()
156  ^ address.ServiceUrl.GetHashCode()
157  ;
158 
159  return code;
160  }
161  }
162 }
Namespace for the Microsoft Bot Connector SDK.
ConversationAccount Conversation
Address for the conversation that this activity is associated with
Definition: IActivity.cs:49
override bool Equals(object other)
Definition: Address.cs:105
string ServiceUrl
ServiceUrl
Definition: IActivity.cs:24
string ChannelId
Channel this activity is associated with
Definition: IActivity.cs:39
string Id
Channel id for the user or bot on this channel (Example: joe@smith.com, or or 123456) ...
ChannelAccount Recipient
Address that received the message
Definition: IActivity.cs:54
Namespace for internal machinery that is not useful for most developers and may change in the future...
The key that minimally and completely identifies a bot&#39;s conversation with a user on a channel...
Definition: Address.cs:45
bool Equals(IAddress other)
Definition: Address.cs:94
Compare two Address instances for equality, excluding the user information.
Definition: Address.cs:134
Shared properties for all activities
Definition: IActivity.cs:9
Address(string botId, string channelId, string userId, string conversationId, string serviceUrl)
Definition: Address.cs:74
ChannelAccount From
Sender address data
Definition: IActivity.cs:44
static Address FromActivity(IActivity activity)
Definition: Address.cs:60
string Id
Channel id for the user or bot on this channel (Example: joe@smith.com, or or 123456) ...
Namespace for the internal fibers machinery that is not useful for most developers and may change in ...
The key that minimally and completely identifies a bot&#39;s conversation with a user on a channel...
Definition: Address.cs:58
Root namespace for the Microsoft Bot Builder SDK.