So David has brought peace to Israel on all sides, he has set up an organized government, and is establishing his kingdom. He seems to be trying to settle debts he felt he owed or to share God's love. Whatever the initial motivation, he started with Mephibosheth, to honor his beloved friend Jonathan, and that went well.
Then, for some unstated reason, David remembered a kindness paid by the king of Ammon, maybe while David was running form Saul. When that king died, his son took over and David sent men to greet him and offer him kindness in return. It is a little odd that Ammon is listed in those that David defeated just a few chapters earlier in II Samuel 8:12. But maybe that is where some kindness was paid? (Ryrie is saying that this is the battle mentioned in II Samuel 8:12. So I have to remember that the timeline isn't always perfectly linear.)
It did not go well when David's men reached out. The king's princes convinced him that David's men were there to spy, so he shaved half of their beards (a humiliation in that culture) and stripped half of their clothes (a humiliation in almost any culture).
Next, according to most versions I looked at it says that "David sent to meet them." It's an odd wording. It sort of sounds like he met them; but it also sounds like he sent messengers, which is what the NIV version says... Either way, he knew they were humiliated and couldn't show back up in Jerusalem with half beards, so he told them to stay in Jericho until their beards grew back.
So Ammon catches wind that David is not happy with them and they pay 10's of thousands of mercenaries to take on Israel at Jerusalem.
David's military leader, Joab and his brother organize a defense and agree to back one another up.
It doesn't exactly show David seeking the Lord's will before sending the men originally to Ammon, but it doesn't always include that. However, when Joab and his brother are preparing the defense, they put the battle into the Lord's hands in verse 10:12.
When the battle was underway, the mercenaries fled, so then the Ammonites fled. Then they regrouped and this time David gathered all of Israel together and crossed the Jordan to put end to the whole conflict. At that point, those fighting against him surrendered and became servants of Israel.
II Samuel 10-Wiersbe
Wiresbe states that "showing kindness" can also be translated, "make covenant", so David might have been trying to make a treaty with Ammon. Which makes sense now that I know the timeline is a bit off with this story being the issue noted in 8:12. He make network connections with those outside of Israel when he was in exile and may have been following up on that in a friendly manner while subduing his enemies on his other sides.
Wiersbe also explains that the things the Ammon king did to the messengers was what would be expected of a prisoner of war.