Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Tcl) BatchModify - Add a Label to each Message in Search ResultsSearchs GMail for messages meeting a criteria and adds a label to each message found.
load ./chilkat.dll # This example requires the Chilkat API to have been previously unlocked. # See Global Unlock Sample for sample code. set http [new_CkHttp] CkHttp_put_AuthToken $http "GMAIL-ACCESS-TOKEN" set userId "me" CkHttp_SetUrlVar $http "userId" $userId set query "subject:questions" CkHttp_SetUrlVar $http "query" $query set url "https://www.googleapis.com/gmail/v1/users/{$userId}/messages?q={$query}" set sb [new_CkStringBuilder] set success [CkHttp_QuickGetSb $http $url $sb] if {$success != 1} then { puts [CkHttp_lastErrorText $http] delete_CkHttp $http delete_CkStringBuilder $sb exit } set json [new_CkJsonObject] CkJsonObject_LoadSb $json $sb CkJsonObject_put_EmitCompact $json 0 puts [CkJsonObject_emit $json] if {[CkHttp_get_LastStatus $http] != 200} then { puts "Failed." delete_CkHttp $http delete_CkStringBuilder $sb delete_CkJsonObject $json exit } # If successful, the received JSON looks like this: # { # "messages": [ # { # "id": "166f583051d36144", # "threadId": "166f583051d36144" # }, # { # "id": "166f5815e1f36144", # "threadId": "166f5815e1f36144" # }, # ... # { # "id": "166f580639e36144", # "threadId": "166f580639e36144" # }, # { # "id": "15dbc2e28ec789c6", # "threadId": "15dbc2e28ec789c6" # } # ], # "nextPageToken": "13434766102274844688", # "resultSizeEstimate": 103 # } # # Next, we'll be sending an HTTP POST to add the label "questions" to each message in the # search results. The JSON to be sent for the batchModify is this: # { # "ids": [ # string # ], # "addLabelIds": [ # string # ], # "removeLabelIds": [ # string # ] # } # We'll omit "removeLabelIds" because we're not removing any labels. # We are parsing the JSON search results, and at the same time building the batchModify JSON. set json2 [new_CkJsonObject] set i 0 set numMessages [CkJsonObject_SizeOfArray $json "messages"] while {$i < $numMessages} { CkJsonObject_put_I $json $i set id [CkJsonObject_stringOf $json "messages[i].id"] CkJsonObject_put_I $json2 $i CkJsonObject_UpdateString $json2 "ids[i]" $id set i [expr $i + 1] } # We need the id of the label (not the name). # I know the name of the label is "questions", but I need to know the id. # See this example: Get Label Id by Name # The id of my label named "questions" is "Label_43" set labelId "Label_43" CkJsonObject_UpdateString $json2 "addLabelIds[0]" $labelId CkJsonObject_UpdateNewArray $json2 "removeLabelIds" CkJsonObject_put_EmitCompact $json2 0 puts [CkJsonObject_emit $json2] # Send the batchModify set url "https://www.googleapis.com/gmail/v1/users/{$userId}/messages/batchModify" # resp is a CkHttpResponse set resp [CkHttp_PostJson3 $http $url "application/json" $json2] if {[CkHttp_get_LastMethodSuccess $http] != 1} then { puts [CkHttp_lastErrorText $http] delete_CkHttp $http delete_CkStringBuilder $sb delete_CkJsonObject $json delete_CkJsonObject $json2 exit } puts "status = [CkHttpResponse_get_StatusCode $resp]" # A 204 response status indicate success. if {[CkHttpResponse_get_StatusCode $resp] != 204} then { puts [CkHttpResponse_bodyStr $resp] puts "Failed." delete_CkHttpResponse $resp delete_CkHttp $http delete_CkStringBuilder $sb delete_CkJsonObject $json delete_CkJsonObject $json2 exit } # The 204 response has an empty response body.. delete_CkHttpResponse $resp puts "BatchModify success!" delete_CkHttp $http delete_CkStringBuilder $sb delete_CkJsonObject $json delete_CkJsonObject $json2 |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.