Yammer API get/messages.json returns incomplete results - yammer

When I compare the data returned by get/messages.json to the data retrieved using the export in yammer. get/messages.json returns 6,300 records. The data export returns 10,469 records. Similar to this post I too am using the older_than parameter. There was a comment in that post that suggested a rate limit issue. I can assure you I am not exceeding the rate limit as I pause for 15 seconds after every 10 requests.
To get my incomplete 6,300 rows I...
Use the export API to get a list of all of the groups
Loop through that list of groups and download the messages for each group using https://www.yammer.com/api/v1/messages/in_group/:group_id.json
Then I use https://www.yammer.com/api/v1/messages.json with the older_than parameter to get all of the messages in the All Company feed.
The issue appears to be with step 3.
Here is the code related to step 3 outlined above:
Sub GetAllCompanyMessages()
Try
Console.WriteLine("Getting All Company Messages")
If File.Exists(allCompanyPath) Then
'delete previous
If Directory.Exists(allCompanyPath) Then
For Each oldFile As String In Directory.GetFiles(allCompanyPath)
File.Delete(oldFile)
Next
Directory.Delete(allCompanyPath)
End If
'create dir
Directory.CreateDirectory(allCompanyPath)
Else
'create dir
Directory.CreateDirectory(allCompanyPath)
'Throw New Exception("Yammer Data Export Zip Download Failed")
End If
'get first group of messages
Console.WriteLine("Getting All Company Batch 1")
Dim client As New WebClient()
client.Headers.Add("Authorization", "Bearer " & accessToken)
client.DownloadFile(allCompanyMessagesURL, allCompanyPath & "1.json")
'getOlderThanID
Dim olderThanID As Int32 = getOlderThanID(allCompanyPath & "1.json")
'get remaining messages in batches of 20
Dim i As Int32 = 2
'Dim prevOlderThanID As Int32 = 0
Dim nextOlderThanID As Int32 = olderThanID
Do Until i = 0
Console.WriteLine("Getting All Company Batch " & i & " olderthanID " & nextOlderThanID)
client = Nothing
client = New WebClient()
client.Headers.Add("Authorization", "Bearer " & accessToken)
client.DownloadFile(allCompanyMessagesURL & "?older_than=" & nextOlderThanID, allCompanyPath & i & ".json")
'prevOlderThanID = nextOlderThanID
nextOlderThanID = getOlderThanID(allCompanyPath & i & ".json")
i = i + 1
If nextOlderThanID = 0 Then
'exit loop
i = 0
End If
' HANDLES 10 REQUESTS IN 10 SECONDS LIMIT
If i >= 10 Then
If i Mod 10 = 0 Then
' CAUSES APP TO WAIT 15 SECONDS AFTER EVERY 10th REQUEST
Console.WriteLine("Sleeping for 15 seconds")
System.Threading.Thread.Sleep(15000)
End If
End If
Loop
Console.WriteLine("Concatenating All Company Batches")
Dim masterJobject As New JObject
masterJobject = JObject.Parse("{""messages"":[]}")
For Each path As String In Directory.GetFiles(allCompanyPath, "*.json")
Console.WriteLine("Concatenating All Company Batch: " & path)
'open each json get messages object and append
Dim jObj As JObject = JObject.Parse(File.ReadAllText(path))
Dim jms As New JsonMergeSettings
'beh 5.24.17 jms.MergeArrayHandling = MergeArrayHandling.Union
jms.MergeArrayHandling = MergeArrayHandling.Concat
masterJobject.Merge(jObj, jms)
'File.Delete(path)
Next
Console.WriteLine("Building Yammer-All-Company-Messages.json")
File.WriteAllText(outputJSONpath & "Yammer-All-Company-Messages.json", "{ ""messages"":" & masterJobject("messages").ToString() & "}")
Catch ex As Exception
ErrorHandler("ERROR GetAllCompanyMessages: " & ex.Message)
End Try
End Sub
Function getOlderThanID(ByVal jsonPath As String) As Int32
Dim result As Int32 = 0
Try
Dim jObj As New JObject
jObj = JObject.Parse(File.ReadAllText(jsonPath))
If CBool(jObj("meta")("older_available")) = True Then
If Not IsNothing(jObj("messages").Last()("id")) Then
result = jObj("messages").Last()("id")
End If
End If
Catch ex As Exception
ErrorHandler("ERROR getOlderThanID: " & ex.Message)
End Try
Return result
End Function
I would appreciate any insight on what the issue might be with the get/messages.json API endpoint, and how I might modify my code to resolve this issue.

There are technical limits on the number of items that will be returned from the REST API for messages. These were designed for client applications that need recent data. The best option would be use individual calls to the message endpoint from the REST API (api_url field) to fill in any gaps in your archive. Make sure everything is stored in a persistent fashion.

Related

External incoming mail marked as draft and unsent

My question is similar to but not the same to the one below,
Mark a mailitem as sent (VBA outlook)
Basically, something (AV, bug in Outlook or Exchange or both), has modified hundreds of incoming (external emails) to a particular user as drafts and now appear as unsent. This means the user cannot reply to these messages and the suggested alternative of copying and pasting looks very unprofessional and confusing to the user's clients. Thankfully whatever was causing it stopped but the damage is done.
I need some way to modify the PR_MESSAGE_FLAGS programmatically. I am comfortable with VB script, VBA, VB.Net and even C#/C++ but I am coming up empty for how to do it.
Should it matter, the server is Exchange 2013 and client is Outlook 2010 or 2016 (32 or 64bit). The entire mailbox has been exported to PST and can be worked on offline if that helps. :)
Based on Dmitry's answer, here is the code that clones the old messages and marks them as sent so they can be replied to.
Only concern with it is that it seems to be breaking Conversations.
Dim mysession
Sub doFixDrafts()
log " Starting scan!"
Set mysession = CreateObject("Redemption.RDOSession")
mysession.Logon
Const sRootFolder = "\\Mailbox\Inbox"
Set oRootFolder = mysession.getfolderfrompath(sRootFolder)
'Set oRootFolder = mysession.PickFolder
doCleanupFolder oRootFolder, sRootFolder
log "Scan complete!!"
End Sub
Sub doCleanupFolder(oFolder, sFolder)
Dim c: c = 0
Dim i: i = 0
Dim tc: tc = Format(oFolder.Items.Count, "0000")
'Get start timestamp so we can report in at regular intervals...
Dim st: st = Now()
log "Checking... " & sFolder
Dim aMsgIDs()
'Make a list of 'unsent' messages
For Each Item In oFolder.Items
i = i + 1
If Not Item.Sent Then
c = c + 1
msgID = Item.EntryID
ReDim Preserve aMsgIDs(1 To c)
aMsgIDs(c) = msgID
c = Format(c, "0000")
End If
'Give update for large folders...
ct = Now()
td = DateDiff("s", st, ct)
If td > 15 Then
log c & "/" & i & "/" & tc & " so far..."
st = ct
End If
DoEvents
Next
c = Format(c, "0000")
log c & "," & tc & "," & sFolder
'Fix the corrupt messages
For m = 1 To CInt(c)
Set badMsg = mysession.GetMessageFromID(aMsgIDs(m))
sSender = badMsg.Sender
sSubject = badMsg.Subject
dSentDate = badMsg.SentOn
Set newMsg = oFolder.Items.Add("IPM.Note")
newMsg.Sent = True
badMsg.CopyTo (newMsg)
newMsg.Save
badMsg.Delete
Dim a As String
a = Format(m, "0000") & "," & sSender & ","
a = a & Chr(34) & sSubject & Chr(34) & ","
a = a & Chr(34) & dSentDate & Chr(34)
log a
DoEvents
Next m
For Each Item In oFolder.Folders
doCleanupFolder Item, sFolder & "\" & Item.Name
Next
End Sub
Sub log(s As String)
d = Format(Now(), "yyyy-mm-dd hh:mm:ss")
t = d & " " & s
Debug.Print t
Const logfile = "c:\temp\fixdrafts.txt"
Open logfile For Append As #1
Print #1, t
Close #1
End Sub
The answer is still the same - on the low (Extended MAPI) level, sent/unsent status (MSGFLAG_UNSENT bit in the PR_MESSAGE_FLAGS property) can only be changed before the item is saved for the very first time.
Outlook Object Model is subject to the same limitation of course, and the only way to create an item in the sent state is to create a PostItem object - it is created in the sent state. You will then need to change the message class back to IPM.Note and remove the icon related properties to make sure the item looks right.
Redemption (I am its author) lets you change the item's state (RDOMail.Sent is read/write before the first call to Save).
It should be pretty easy to create copies of existing unsent messages in the sent state - loop through the problematic messages (it is better to avoid using "for each" if you will be creating new items in the same folder - your "for each" loop will start picking up new messages. Loop through the messages first and store their entry ids in a list or array), create new item using Redemption (RDOFolder.Items.Add), set the Sent property to true (RDOMail.Sent = true), open the problematic message by its entry ids (RDOSession.GetMessageFromID), copy the problematic message into the new message using RDOMail.CopyTo(AnotherRDOMailObject), call RDOMail.Save on the new message and RDOMail.Delete on the old message.

Using One Service to Stop Another

I have written a windows service application whose purpose is to stop and later start another service.
I am getting this error when I try the stop the other service.
Failed in StopTheProcess Cannot stop CaseMixProcessManager service on computer '.'.System.ComponentModel.Win32Exception (0x80004005): The service cannot accept control messages at this time
I can see that the status of the service I am trying to stop is 4 for running. The user has the privileges to stop and start the service.
I still don't get what Harry is suggesting so I am posting the whole program as there is not that much to it. Perhaps Harry or someone else can suggest how to change it.
Public Sub StartProcessing()
Try
ClassLibraries.clsUtilities.LogSource = "ServiceController"
ClassLibraries.clsUtilities.WriteLog("Service Controller Started " & cGlobals.m_Version, EventLogEntryType.Information)
m_StartHour = GetConfiguation("StartHour")
m_StopHour = GetConfiguation("StopHour")
m_ServiceName = GetConfiguation("ServiceName")
m_Service = New ServiceProcess.ServiceController(m_ServiceName)
If StartOrStop() = True Then
WaitToStart()
Else
WaitToStop()
End If
Catch ex As Exception
ClassLibraries.clsUtilities.WriteLog("Failed in Start Processing " & ex.Message, EventLogEntryType.Error)
End Try
End Sub
Private Function FigureNextTime(p_Hour As String) As Boolean
Dim _TimeToRestart As DateTime
Dim _TodayString As String
Dim _RestartString As String
Dim _NowTime As DateTime
Dim _Result As Int32
Try
ClassLibraries.clsUtilities.WriteLog("FigureNextTime " & p_Hour, EventLogEntryType.Information)
' we need to restart the process automatically at a certain hour
If p_Hour = "N" Then
Return False
End If
If IsNumeric(p_Hour) = False Then
Return False
End If
p_Hour = p_Hour & ":00:00"
_TodayString = Convert.ToString(DateTime.Today)
_RestartString = _TodayString.Replace("12:00:00", p_Hour)
_RestartString = _RestartString.Replace("AM", "")
_TimeToRestart = Convert.ToDateTime(_RestartString)
' next compare the two dates see if the time has passed
_NowTime = (DateTime.Now)
_Result = DateTime.Compare(_NowTime, _TimeToRestart)
If (_Result >= 0) Then
' we add one day to the restart time
_TimeToRestart = _TimeToRestart.AddDays(1)
End If
m_MinutesDifference = DateDiff(DateInterval.Minute, _NowTime, _TimeToRestart)
' figure the millseconds until it's time to start the process
m_MilliSeconds = m_MinutesDifference * 60000
Return True
Catch ex As Exception
ClassLibraries.clsUtilities.WriteLog("Failed in FigureNextTime " & ex.Message, EventLogEntryType.Error)
Return False
End Try
End Function
Private Function GetConfiguation(p_KeyName As String) As String
Return ConfigurationManager.AppSettings(p_KeyName)
End Function
Private Sub WaitToStart()
Try
ClassLibraries.clsUtilities.WriteLog("WaitToStart", EventLogEntryType.Information)
If FigureNextTime(m_StartHour) = False Then
Exit Sub
End If
ClassLibraries.clsUtilities.WriteLog("Will start in " & m_MinutesDifference & " Minutes", EventLogEntryType.Information)
m_svcTimer = New System.Timers.Timer(m_MilliSeconds)
' Hook up the Elapsed event for the timer.
AddHandler m_svcTimer.Elapsed, AddressOf StartTheProcess
' Set the Interval
m_svcTimer.Interval = m_MilliSeconds
m_svcTimer.Enabled = True
m_svcTimer.Start()
Catch ex As Exception
ClassLibraries.clsUtilities.WriteLog("Failed in WaitToStart " & ex.Message, EventLogEntryType.Error)
End Try
End Sub
I figured out the problem and I want to let future readers of this thread know about it.
Harry Johnston was on the right track suggestion a separate thread which I never hear of before. But the thread belonged in the Service I was trying to stop not the service that was doing the stopping. Because the On Start routine never ended I could stop the process programmatically. I put the bulk of what I am doing in a separate thread.

IOException Was Unhandled Error Help for Newbie

You'll have to excuse me for a repeat question but I just can't seem to understand the responses others have been posting. My coding skills are very limited (barely a year in vb 2006 and vs 2010)
I know that the sr has opened too many of the same file but I cant figure out a fix for it. Explaining it to me in simple concepts would be very helpful. Once again sorry for the newby-ness.
Project Explanation: Create a bowling league stats keeper. cmdRegisterBowler adds bowler name to a dat file (bowlers.dat). cmdEnterScores will write name and stats to games.dat file.
Private Sub cmdRegisterBowler_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRegisterBowler.Click
'BOWLER REGISTRATION
Dim Person As String
Dim Team As String
txtName.Text.ToUpper()
txtTeam.Text.ToUpper()
Person = txtName.Text
Team = txtTeam.Text
If Person <> "" And Team <> "" Then
If IsInFile(Person & " " & Team) = True Then
MessageBox.Show(Person & " is already in the file.", "Error")
txtName.Clear()
txtTeam.Clear()
txtName.Focus()
Else
'swb = stream writer bowlers file
Dim swb As IO.StreamWriter = IO.File.AppendText("bowlers.dat")
swb.WriteLine(Person & " " & Team)
swb.Close()
MessageBox.Show(Person & " has been added to the file.", "Information Added")
txtName.Clear()
txtTeam.Clear()
txtName.Focus()
End If
Else
MessageBox.Show("You must enter a name.", "Information Incomplete")
End If
End Sub
Function IsInFile(ByVal person As String) As String
If IO.File.Exists("bowlers.dat") Then
Dim sr As IO.StreamReader = IO.File.OpenText("bowlers.dat")
Dim individual As String
Do Until sr.EndOfStream
individual = sr.ReadLine
If individual = person Then
Return True
sr.Close()
End If
Loop
sr.Close()
End If
Return False
End Function
Private Sub cmdEnterScores_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdEnterScores.Click
'SCORE ENTRY
Dim Person As String
Dim Team As String
Dim Score1 As Double
Dim Score2 As Double
Dim Score3 As Double
Dim Average As Double
Dim Display As String = "{0,-10} {1,10} {2,20} {3,10} {4,10} {5,10} {6,10}"
Dim Total As Double
Person = txtName2.Text
Team = txtTeam2.Text
Score1 = Val(txtFirstGame.Text)
Score2 = Val(txtSecondGame.Text)
Score3 = Val(txtThirdGame.Text)
Total = Score1 + Score2 + Score3
Average = Total / 3
Dim swb As IO.StreamWriter = IO.File.AppendText("bowlers.dat")
'Checks for blank entries
If Person <> "" Then
'Checks to see if person is registered
If IsNotInFile(Person) Then
Dim Response As Integer
' Displays a message box with the yes and no options to register bowler.
Response = MsgBox(Prompt:="The bowler you have entered is currently not registered. Would you like to do so?", Buttons:=vbYesNo)
'Yes button was selected. Bowler is then registered to bowlers.dat
If Response = vbYes Then
''swb = stream writer bowlers file
swb.WriteLine(Person & " " & Team)
swb.Close()
MessageBox.Show(Person & " has been added to the file.", "Information Added")
'The no button was selected. Focus is set to bowler registration group box
Else : Response = vbNo
txtName2.Clear()
txtTeam2.Clear()
txtFirstGame.Clear()
txtSecondGame.Clear()
txtThirdGame.Clear()
txtName.Focus()
End If
'If no then clears score entry group box and sets focus back to bowler registr
txtName.Clear()
txtName.Focus()
Else
'Write scores to games.dat file
'swg = stream writer games file
Dim swg As IO.StreamWriter = IO.File.AppendText("games.dat")
swg.WriteLine(Name & " " & Team & " " & Score1 & " " & Score2 & " " & Score3)
swg.Close()
MessageBox.Show(Person & "'s stats added to the file.", "Information Added")
txtName.Clear()
txtName.Focus()
Dim Display2 As String = "{0,-10} {1,10} {2,20} {3,10} {4,10} {5,10} {6,10}"
lstDisplay.Items.Add(String.Format(Display2, Person, Team, Score1, Score2, Score3, Total, Average))
End If
Else
MessageBox.Show("You must enter a name.", "Information Incomplete")
End If
End Sub
Function IsNotInFile(ByVal person As String) As String
If IO.File.Exists("bowlers.dat") Then
Dim sr As IO.StreamReader = IO.File.OpenText("bowlers.dat")
Dim individual As String
Do Until sr.EndOfStream
individual = sr.ReadLine
If individual <> person Then
Return True
sr.Close()
End If
Loop
sr.Close()
End If
Return False
End Function
Heres all my code. Thanks for any help in advance.
I haven't used Visual Basic in quite a while, but I believe the solution is simple.
Basically, your interactions with the System.IO library are currently very optimistic. You should always wrap IO interactions within a TRY...CATCH...END TRY block.
Check it out here: MSDN Example of StreamReader.ReadLine
To illustrate more clearly my meaning, I've altered one of your functions here:
Function IsInFile(ByVal person As String) As String
Try
If IO.File.Exists("bowlers.dat") Then
Dim sr As IO.StreamReader = IO.File.OpenText("bowlers.dat")
Dim individual As String
Do Until sr.EndOfStream
individual = sr.ReadLine
If individual = person Then
Return True
sr.Close()
End If
Loop
sr.Close()
End If
Return False
Catch ie as IOException
MessageBox.Show("Failed to search for " & person & " in file.", "Error")
Return false
End Try
End Function
I'd apply similar logic to surround all your interactions with Files within your code. I'd also recommend against using MessageBox to report errors -- it's okay for small projects, but for anything larger you're going to want to look up using a logging framework. Or build a debug console into your application that you can selective turn on or off via configuration.

Writing GetResponseStream to file is taking long

I have made a SSRS Bulk reporter that queries a report from SSRS using a HTTPWebRequest and a HTTPWebResponse to get the stream and save it as a file to a PDF.
However, the client has complained that this is taking long. I have tested it, and this function is taking +- 15.5seconds. If I put a break in-between the GetResponseStream and the file-writer, and I wait for about 3 seconds before stepping to the next part, it shaves 4 seconds off the total time?
Can anyone explain this/or give some advise as to make it a bit faster?
This is the function:
Public Function ExportReport(ByVal QueryStringParameters As String, ByVal FileName As String) As String
Dim PDFName As String = ReportFolderPath & FileName & ".pdf"
'Create a http request or connecting to the report server.
Dim ReportHTTPRequest As HttpWebRequest = Nothing
'Create a http response to catch the data stream returned from the http request.
Dim ReportHTTPResponse As HttpWebResponse = Nothing
'Create a stream to read the binary data from the http reponse.
Dim ReportStream As Stream = Nothing
Dim ReportFileStream As FileStream = Nothing
'Create an array of bytes to get the binary data from the stream.
Dim ReportBytes As Byte()
Dim ReportBuffer As Integer = 204800
Dim ReportBytesRead As Integer = 0
'Create a webrequest to get the report with all the report parameters included.
ReportHTTPRequest = WebRequest.Create(ReportServerURL & QueryStringParameters)
ReportHTTPRequest.Credentials = CredentialCache.DefaultCredentials
'Get the response from the request.
ReportHTTPResponse = ReportHTTPRequest.GetResponse()
'Read the binary stream from the http response.
ReportStream = ReportHTTPResponse.GetResponseStream()
ReportBytes = New Byte(ReportBuffer) {}
ReportBytesRead = ReportStream.Read(ReportBytes, 0, ReportBuffer)
ReportFileStream = New FileStream(PDFName, FileMode.Create)
Do While ReportStream.CanRead And ReportBytesRead > 0
ReportFileStream.Write(ReportBytes, 0, ReportBytesRead)
ReportBytesRead = ReportStream.Read(ReportBytes, 0, ReportBuffer)
Loop
ReportHTTPResponse.Close()
ReportStream.Close()
ReportFileStream.Close()
Return PDFName
End Function
a magic thing that worked for me, try a buffer size of 32768

VB dataset issue

The idea was to create a message box that stores my user name, message, and post datetime into the database as messages are sent.
Soon came to realize, what if the user changed his name?
So I decided to use the user id (icn) to identify the message poster instead. However, my chunk of codes keep giving me the same error. Says that there are no rows in the dataset ds2.
I've tried my Query on my SQL and it works perfectly so I really really need help to spot the error in my chunk of codes here.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim name As String
Dim icn As String
Dim message As String
Dim time As String
Dim tags As String = ""
Dim strConn As System.Configuration.ConnectionStringSettings
strConn = ConfigurationManager.ConnectionStrings("ufadb")
Dim conn As SqlConnection = New SqlConnection(strConn.ToString())
Dim cmd As New SqlCommand("Select * From Message", conn)
Dim daMessages As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim ds As New DataSet
cmd.Connection.Open()
daMessages.Fill(ds, "Messages")
cmd.Connection.Close()
If ds.Tables("Messages").Rows.Count > 0 Then
Dim n As Integer = ds.Tables("Messages").Rows.Count
Dim i As Integer
For i = 0 To n - 1
icn = ds.Tables("Messages").Rows(i).Item("icn")
Dim cmd2 As New SqlCommand("SELECT name FROM Member inner join Message ON Member.icn = Message.icn WHERE message.icn = #icn", conn)
cmd2.Parameters.AddWithValue("#icn", icn)
Dim daName As SqlDataAdapter = New SqlDataAdapter(cmd2)
Dim ds2 As New DataSet
cmd2.Connection.Open()
daName.Fill(ds2, "PosterName")
cmd2.Connection.Close()
name = ds2.Tables("PosterName").Rows(0).Item("name")
message = ds.Tables("Messages").Rows(i).Item("message")
time = ds.Tables("Messages").Rows(i).Item("timePosted")
tags = time + vbCrLf + name + ": " + vbCrLf + message + vbCrLf + tags
Next
txtBoard.Text = tags
Else
txtBoard.Text = "nothing to display"
End If
End Sub
Would it be more efficient to combine both cmd and cmd2, such that cmd becomes
SELECT msg.*,mem.Name FROM Message msg INNER JOIN Member mem ON msg.icn = mem.icn ?
This way, your Member.name would be in the same dataset as your Messages table, making your code much cleaner.
-Joel

Resources