Share via

Yammer Message API with Attachment

Anonymous
2017-08-17T06:23:35+00:00

Hi All,

I have tried the following PowerShell script to post a message in Yammer. It is working as per expected if no attachment within the post.

Unfortunately, I keep getting an error with code 400 if I post a message with attachment by using following script.

HTTP Status Code: 400

Message: The remote server returned an error: (400) Bad Request.

Hope someone expert here can give me advice.

Thanks,

Eric

function New-YammerMessage
(
    [Parameter(Mandatory=$true)][string]$messageBody,
    [string]$groupID,
    [string]$repliedToID,
    [string]$directToID,
    [string]$announcementTitle,
    [string]$attachment,
    [string[]]$topic
    
){
   
    try {
      
            $requestBody = @{}
            $requestBody.body = $messageBody
                if ($groupID){ 
                    $requestBody.group_id = $groupID 
				if ($directToID) {
					log "DirectedToID and GroupID cannot be execute in the same request; message will be posted in the group"
					$directToID = ""
				}
				if ($repliedToID) {
					$message = Get-YammerMessage -messageID $repliedToID
					if ($message.group_id -ne $groupID) {
						$message = "The message " + $repliedToID + " is not part of group " + $groupID + ". message will be posted in the group."
						log $message
						$repliedToID = ""
					}
				}
                }
			
                if ($directToID){ 
				$requestBody.direct_to_id = $directToID 
			} 
			
                if ($topic.Length -gt 0) { 
				$topicNum = 1;
				foreach ($tag in $topic){
					$topicN = 'topic'+$topicNum
					$requestBody.$topicN = $tag
					$topicNum +=1
				}
			}
			
                if ($repliedToID) { 
				$requestBody.replied_to_id = $repliedToID 
			}
			
                if ($announcementTitle) {
				$requestBody.message_type = 'announcement'
				$requestBody.title = $announcementTitle
			}
			
			if ($attachment) {
								
				[System.IO.FileInfo] $filename = $attachment
				$folderid = 0
				$boundary = [guid]::NewGuid().ToString()
				$contenttype = Get-ContentType $filename

				$filebody = [System.IO.File]::ReadAllBytes($filename)
				$enc = [System.Text.Encoding]::GetEncoding('utf-8')
				$filebodytemplate = $enc.GetString($filebody)
				# creating the formdata manually
				[System.Text.StringBuilder]$contents = New-Object System.Text.StringBuilder
				[void]$contents.AppendLine()
				[void]$contents.AppendLine("--$boundary")
				[void]$contents.AppendLine("Content-Dis-data; name=""file""; filename=""$($filename.Name)""")
				[void]$contents.AppendLine()
				[void]$contents.AppendLine($filebodytemplate)
				[void]$contents.AppendLine("--$boundary")
				[void]$contents.AppendLine("Content-Type: $contenttype; charset=utf-8")
				[void]$contents.AppendLine("Content-Dis-data; name=""metadata""")
				[void]$contents.AppendLine()
				[void]$contents.AppendLine("{""name"":""$($filename.Name)"",""parent"":{""id"":""$folderid""}}")
				[void]$contents.AppendLine()
				[void]$contents.AppendLine("--$boundary--")
				
				$requestBody.attachment1 = $contents.ToString()
			}
                          
            $requestBody = ConvertTo-Json $requestBody
            $requestBody
            $target = 'https://www.yammer.com/api/v1/messages.json'
            
		if ($attachment) {
			$response = Invoke-RestMethod $target -Headers $headers -Method POST -Body $requestBody -ContentType "multipart/form-data;boundary=$boundary"
		}
		else {
			$response = Invoke-RestMethod $target -Headers $headers -Method POST -Body $requestBody -ContentType application/json
		}
    } 
    Catch [system.exception] {
        $message = "Error: Failed to execute request = " + $target
	log $message red
	$message = "HTTP Status Code: " + $_.Exception.Response.StatusCode.Value__
	log $message red
	$message = "Message: " + $_.Exception.Message
	log $message red
    }

    Return $response.messages

}
Microsoft 365 and Office | Install, redeem, activate | For home | Other

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

1 answer

Sort by: Most helpful
  1. Anonymous
    2017-09-01T00:51:11+00:00

    Hello Eric,

    This is a new feature in Yammer, currently it  may have not rolled out for all the users.

    All Office 365 updates are thoroughly tested and evaluated for performance and scalability before they are released to the public.

    For significant updates, Office customers are initially notified by the Office 365 public roadmap. As an update gets closer to rolling out, it is communicated through your Office 365 Message Center.

    The update is then applied to users who have selected First Release. Once it is determined that the feature is ready for broader release, it's applied to everyone else, including people who have selected Standard Release.

    To receive the latest updates, we request to enable First releases for your account.

    First Release : With this option, you and your users can be the first to see the latest updates and help shape the product by providing early feedback. You can choose to have individuals or the entire organization receive updates early.

    Please refer the Microsoft Article for more information Set up the Standard or First Release options in Office 365

    Regards,

    Sushil Dhiwa.

    Microsoft Yammer support team.

    Was this answer helpful?

    0 comments No comments