He got the last laugh.

Bonanno crime-family cap​o Robert “Bobby Ha Ha” Attanasio came out on top Wednesday when a judge sentenced him to just ​six months home confinement for violating the terms of his parole​ when he gabb​ed on the phone with other ​wiseguys.

Prosecutors originally tried to lock up the capo claiming he’d been caught red handed playing in a Staten Island bocce tournament against Gambino ​associate Louis Vallario.

​​

​But it was actually Attanasio’s namesake son who was entered in the ​M​afia-studded games at Angelina’s Restaurant in June and July 2015, defense attorney James Froccaro told Brooklyn federal court judge Nicholas Garaufis.

“So this is a mistake?” Garaufis asked Froccaro and Assistant US Attorney Nicole Argentieri.

Froccaro nodded as he stood next to his hulking, balding client, but Argentieri refused to admit there’d been an error, saying her office had agreed to not pursue the bocce-related allegations if Attanasio copped to the other violations.

The 70-year-old​ Attanasio ​– who served 10 years behind bars for his role in the brutal 1984 rubout of associate Cesare Bonventre​ ​– admitted Wednesday he’d taken part in some 300 calls with former prison pals Vincent “Vinny Goo” Caroleo and Frank “Meatball” Bellatoni.

The convicted wiseguy is not allowed to associate with other mafioso after he was imprisoned in 2006 alongside his brother, Louis “Louie Ha Ha” Attanasio and capo Peter “Rabbit” Calabrese for Bonventre’s murder.

Bonventre’s hacked up remains were found pickled in steel drums secreted away in a New Jersey glue factory after mob boss Joe Massino ordered the hit.

Froccaro tried to convince the court Wednesday that his client’s admission didn’t mean he was turning over on Caroleo or Bellatoni, and had no personal knowledge they were mobbed-up.

“I think that’s a distinction that only matters to a certain segment of people,” Argentieri said dryly.

Garaufis wasn’t buying it either.

“If someone’s name is “Vinny Goo,” or “Vinny Collision,” or “Meatball,” frankly, based on my extensive experience in organized crime cases, these are the kinds of nicknames given to people in the cosa nostra,” he shot back. “I don’t know Vinny Goo. I don’t know Meatball.”

Yet Argentieri and Garaufis suspended their disbelief, and accepted Attanasio’s rat-proof admission, in which he only said: “I had contact with individuals whom the government had alleged were associated with organized crime.”

Froccaro asked Garaufis for home confinement, arguing the men only contacted his client when they learned he had cancer.

“It was just stupid,” the lawyer said.

“I don’t know that there’s an exception for prison for stupidity,” Garaufis responded, noting Attanasio’s wife had also “accused her husband of stupidity” in a letter to the court.

But the argument worked, and the judge let Attanasio off with a warning, 6 months home detention, and 2 years post-release supervision.

“Stay off the phone with anyone who isn’t a close relative,” Garaufis ordered. “Or your priest.”