public static FrayDbTeamsTeam SqlGetSingleTeam(string id)
        {
            FrayDbTeamsTeam rtnItem = null;

            using (SqlConnection sqlConnection = new SqlConnection(ChallongeSQLHelperConnectionString))
            {
                SqlCommand cmd = new SqlCommand(@"SELECT * FROM dbo.DB_TEAMS_TEAM WHERE TEAM_ID = @TeamId", sqlConnection);

                cmd.Parameters.AddWithValue("@TeamId", id);

                sqlConnection.Open();

                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        rtnItem = new FrayDbTeamsTeam();

                        rtnItem.TeamId   = reader["TEAM_ID"].ToString();
                        rtnItem.TeamName = reader["TEAM_NAME"].ToString();
                    }
                }
            }

            return(rtnItem);
        }
        public static FrayDbTeamsTeam SqlSaveTeam(FrayDbTeamsTeam team)
        {
            using (SqlConnection sqlConnection = new SqlConnection(ChallongeSQLHelperConnectionString))
            {
                SqlCommand cmd = new SqlCommand(@"INSERT INTO dbo.DB_TEAMS_TEAM
                (
                    TEAM_ID,
                    TEAM_NAME
                )
                VALUES
                (
                    @TeamId,
                    @TeamName
                )", sqlConnection);

                cmd.Parameters.AddWithValue("@TeamId", team.TeamId);
                cmd.Parameters.AddWithValue("@TeamName", team.TeamName);

                sqlConnection.Open();

                cmd.ExecuteNonQuery();
            }

            return(team);
        }
        public async Task HandleMessage(ConnectorClient connector, Activity activity)
        {
            bool rejected = true;

            ChallongeSQLHelper.ChallongeSQLHelperConnectionString = ConfigurationManager.AppSettings["dbConnection"];
            var channelThing = activity.GetChannelData <TeamsChannelData>();

            FrayDbTeamsTeam existingTeam = ChallongeSQLHelper.SqlGetSingleTeam(channelThing.Team.Id);

            if (existingTeam == null)
            {
                var teamInfo = connector.GetTeamsConnectorClient().Teams.FetchTeamDetails(channelThing.Team.Id);

                var addTeam = new FrayDbTeamsTeam()
                {
                    TeamId   = teamInfo.Id,
                    TeamName = teamInfo.Name
                };

                ChallongeSQLHelper.SqlSaveTeam(addTeam);
                rejected = false;
            }

            //Check to see whether we've already got the channel
            FrayDbTeamsChannel existingChannel = ChallongeSQLHelper.SqlGetSingleChannel(channelThing.Channel.Id, channelThing.Team.Id);

            if (existingChannel == null)
            {
                //If your channel id matches the team id, you're in the autogenerate General
                if (channelThing.Channel.Id.Equals(channelThing.Team.Id))
                {
                    var addChannel = new FrayDbTeamsChannel()
                    {
                        ChannelId   = channelThing.Channel.Id,
                        TeamId      = channelThing.Team.Id,
                        ChannelName = "General",
                        IsPost      = true
                    };

                    ChallongeSQLHelper.SqlSaveChannel(addChannel);
                    rejected = false;
                }
                else
                {
                    //We gotta get the channels and figure out which one we're in
                    var convoList      = connector.GetTeamsConnectorClient().Teams.FetchChannelList(channelThing.Team.Id);
                    var currentChannel = convoList.Conversations.First(x => x.Id.Equals(channelThing.Channel.Id));

                    var addChannel = new FrayDbTeamsChannel()
                    {
                        ChannelId   = currentChannel.Id,
                        TeamId      = channelThing.Team.Id,
                        ChannelName = currentChannel.Name,
                        IsPost      = true
                    };

                    ChallongeSQLHelper.SqlSaveChannel(addChannel);
                    rejected = false;
                }
            }

            Activity reply = null;

            if (rejected)
            {
                reply = activity.CreateReply("That's going to be a hard pass");
            }
            else
            {
                reply = activity.CreateReply("Saved the data");
            }

            await connector.Conversations.ReplyToActivityWithRetriesAsync(reply);
        }