in Flow

How to get all messages in a Yammer group using Microsoft Flow

Limitation

If you are reading this article you are probably trying to get more than 20 messages from a Yammer group using the Get messages in a group action.

To demonstrate this limitation I created a simple flow that gets the messages from a group and then counts the items in the Messages array.

I used a Compose action to output the count. To count the messages, I used the length function: length(body('Get_messages_in_a_group')?['messages'])

The Limit input parameter is correctly set to 1000:

But the count returns only 20:

Workaround

Luckily there's a method which involves a bit of work. Although the workaround might seem overkill just to return more than 20 messages, I believe it's a good exercise to understand what Microsoft Flow is capable of and how it can be used to overcome connector or API limitations.

To get all messages, I will put the Get messages in a group action in a Do until loop and leverage the Older than field to retrieve messages older than the previous last message ID.

The message ID is an integer that is incremented with each message we post on Yammer. When we get the first 20 messages from a Yammer group, we basically look at an array containing the last 20 messages that were posted.

The last message in the array has the oldest ID. I've added an array containing two messages below – yes, I've use bacon ipsum again ­čśÇ.

Notice that the second message in the array has a smaller integer (1232891960) than the first one (1232891976)

[{
"id": 1232891976,
"sender_id": 1687914702,
"replied_to_id": null,
"created_at": "2019/02/02 20:10:12 +0000",
"network_id": 10542913,
"message_type": "update",
"sender_type": "user",
"url": "https://www.yammer.com/api/v1/messages/1232891976",
"web_url": "https://www.yammer.com/superheroes.onmicrosoft.com/messages/1232891976",
"group_id": 17225930,
"body": {
  "parsed": "Ut shank aliqua officia aliquip bacon turkey incididunt enim ham hock tongue ball tip meatloaf aute alcatra.  Dolore t-bone shoulder ham ground round nostrud bresaola.  Chuck proident tenderloin sint pork chop aliqua.  Culpa id rump, sed consectetur spare ribs ut ex picanha.",
  "plain": "Ut shank aliqua officia aliquip bacon turkey incididunt enim ham hock tongue ball tip meatloaf aute alcatra. Dolore t-bone shoulder ham ground round nostrud bresaola. Chuck proident tenderloin sint pork chop aliqua. Culpa id rump, sed consectetur spare ribs ut ex picanha.",
  "rich": "Ut shank aliqua officia aliquip bacon turkey incididunt enim ham hock tongue ball tip meatloaf aute alcatra.  Dolore t-bone shoulder ham ground round nostrud bresaola.  Chuck proident tenderloin sint pork chop aliqua.  Culpa id rump, sed consectetur spare ribs ut ex picanha."
},
"thread_id": 1232891976,
"client_type": "Microsoft PowerApps and Flow",
"client_url": "https://ms.flow.microsoft.com/en-us/services/shared_yammer/yammer/",
"system_message": false,
"direct_message": false,
"chat_client_sequence": null,
"language": "en",
"notified_user_ids": [],
"privacy": "public",
"attachments": [],
"liked_by": {
  "count": 0,
  "names": []
},
"content_excerpt": "Ut shank aliqua officia aliquip bacon turkey incididunt enim ham hock tongue ball tip meatloaf aute alcatra. Dolore t-bone shoulder ham ground round nostrud bresaola. Chuck proident tenderloin sint pork chop aliqua. Culpa id rump, sed consectetur spare ribs ut ex picanha.",
"group_created_id": 17225930,
"topics": []
},
{
"id": 1232891960,
"sender_id": 1687914702,
"replied_to_id": null,
"created_at": "2019/02/02 20:10:08 +0000",
"network_id": 10542913,
"message_type": "update",
"sender_type": "user",
"url": "https://www.yammer.com/api/v1/messages/1232891960",
"web_url": "https://www.yammer.com/superheroes.onmicrosoft.com/messages/1232891960",
"group_id": 17225930,
"body": {
  "parsed": "Shankle eu culpa turkey tri-tip, tempor drumstick eiusmod bacon ut beef ribs ad.  Corned beef turducken mollit velit est bacon ullamco doner.  Venison ex tempor, ullamco jerky fugiat excepteur prosciutto capicola tail sint dolore short loin.  Beef short loin pastrami boudin cillum.",
  "plain": "Shankle eu culpa turkey tri-tip, tempor drumstick eiusmod bacon ut beef ribs ad. Corned beef turducken mollit velit est bacon ullamco doner. Venison ex tempor, ullamco jerky fugiat excepteur prosciutto capicola tail sint dolore short loin. Beef short loin pastrami boudin cillum.",
  "rich": "Shankle eu culpa turkey tri-tip, tempor drumstick eiusmod bacon ut beef ribs ad.  Corned beef turducken mollit velit est bacon ullamco doner.  Venison ex tempor, ullamco jerky fugiat excepteur prosciutto capicola tail sint dolore short loin.  Beef short loin pastrami boudin cillum."
},
"thread_id": 1232891960,
"client_type": "Microsoft PowerApps and Flow",
"client_url": "https://ms.flow.microsoft.com/en-us/services/shared_yammer/yammer/",
"system_message": false,
"direct_message": false,
"chat_client_sequence": null,
"language": "en",
"notified_user_ids": [],
"privacy": "public",
"attachments": [],
"liked_by": {
  "count": 0,
  "names": []
},
"content_excerpt": "Shankle eu culpa turkey tri-tip, tempor drumstick eiusmod bacon ut beef ribs ad. Corned beef turducken mollit velit est bacon ullamco doner. Venison ex tempor, ullamco jerky fugiat excepteur prosciutto capicola tail sint dolore short loin. Beef short loin pastrami boudin cillum.",
"group_created_id": 17225930,
"topics": []
}]

So, going back to the editor, I am starting with a button trigger and 3 variables:

  • varAllMessages – array variable to store all messages – the initial value is an empty array []
  • varNoMoreMessages – boolean variable that will be set to true if there are no more messages to pull – the initial value is false because I assume that the first pull contains messages
  • varLastMessageID – integer variable to store the last message ID in each loop – the initial value is 0 – it's safe to use it because we can't have IDs smaller than 0

The next step is the Do until loop that does the heavy lifting. The loop will stop when varNoMoreMessages is true.

I've set the Older than field to this expression: if(equals(variables('varLastMessageID'), 0), null, variables('varLastMessageID'))´╗┐

The expression will evaluate if varLastMessageID´╗┐ is equal to 0´╗┐ and return null´╗┐ if true´╗┐ else will return the actual ID stored in varLastMessageID´╗┐

In the first loop iteration, the expression will return null and thus the action will return the first 20 messages it finds in the group. In the next iteration, varLastMessageID will have an ID and thus the action will return the next 20 messages.

The Do until also contains a condition to check if the messages array returned by the Get messages in a group action is empty.

If the array is empty, then we set the varNoMoreMessages variable to true. This will stop the Do until from looping.

If the array contains messages, then we use a Compose action to append the array of messages returned by the Get messages in a group action to the existing array in the varAllMessages variable.

The function we use for this operation is union: union(variables('varAllMessages'), body('Get_messages_in_a_group')?['messages'])

Messages will add up with each iteration and the varAllMessages array will grow larger.

The reason we use the Compose action to append the arrays is because the Set variable action does not support self-referencing.

In the next step we set the varAllMessages variable with the output from the Compose array of messages from union action.

The last step is the Set variable varLastMessageID action which sets the varAllMessages variable to the ID of the last message it finds in the array returned by the Get messages in a group action. This ensures that, with each iteration, we are pulling messages older than the last ID.

If I run the flow and I look at the Do until iterations, I can see that it looped 37 times.

Note that the last iteration doesn't really process any messages, it only sets the varNoMoreMessages variable to true and closes the loop.

Looking at the varAllMessages output, we can see that we have 708 messages. Although the screenshot shows 707, it's actually 708 because the count starts from 0.

You can download this flow from here.

Write a Comment

Comment