<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[Серый форум &mdash; AHK: VK getMessagesUploadServer]]></title>
	<link rel="self" href="https://forum.script-coding.com/extern.php?action=feed&amp;tid=15213&amp;type=atom" />
	<updated>2020-03-02T18:01:57Z</updated>
	<generator>PunBB</generator>
	<id>https://forum.script-coding.com/viewtopic.php?id=15213</id>
		<entry>
			<title type="html"><![CDATA[Re: AHK: VK getMessagesUploadServer]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=138220#p138220" />
			<content type="html"><![CDATA[<p>Работает! Благодарю.</p>]]></content>
			<author>
				<name><![CDATA[kolotilov256]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=40656</uri>
			</author>
			<updated>2020-03-02T18:01:57Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=138220#p138220</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK: VK getMessagesUploadServer]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=138187#p138187" />
			<content type="html"><![CDATA[<p>У меня так отправляет:<br /></p><div class="codebox"><pre><code>id := 123456789
token := &quot;ccgjhgjkhkjbhkb3d454bb6dd4b39ce43640e298a1374b5797eac0a3b05c9b57&quot;
path := &quot;C:\Users\blahblah\Desktop\face.jpg&quot;

HTTP := ComObjCreate(&quot;WinHttp.WinHttpRequest.5.1&quot;)
HTTP.Open(&quot;GET&quot;, &quot;https://api.vk.com/method/photos.getMessagesUploadServer?peer_id=&quot; id &quot;&amp;access_token=&quot; token &quot;&amp;v=5.103&quot;, true)
HTTP.Send()
HTTP.WaitForResponse()
RegexMatch(HTTP.responsetext, &quot;upload_url&quot;&quot;:&quot;&quot;(.+?)&quot;&quot;&quot;, match)
server := StrReplace(match1, &quot;\/&quot;, &quot;/&quot;)
objParam := {photo: [path]}
CreateFormData(PostData, hdr_ContentType, objParam)
HTTP.Open(&quot;POST&quot;, server, true)
HTTP.SetRequestHeader(&quot;Content-Type&quot;, hdr_ContentType)
HTTP.Send(PostData)
HTTP.WaitForResponse()
RegexMatch(HTTP.responsetext, &quot;\{&quot;&quot;server&quot;&quot;:(.+?),&quot;&quot;photo&quot;&quot;:&quot;&quot;(.+?)&quot;&quot;,&quot;&quot;hash&quot;&quot;:&quot;&quot;(.+?)&quot;&quot;&quot;, match)
match2 := StrReplace(match2, &quot;\&quot;&quot;&quot;, &quot;&quot;&quot;&quot;)
HTTP.Open(&quot;GET&quot;, &quot;https://api.vk.com/method/photos.saveMessagesPhoto?server=&quot; match1 &quot;&amp;photo=&quot; match2 &quot;&amp;hash=&quot; match3 &quot;&amp;access_token=&quot; token &quot;&amp;v=5.103&quot;, true)
HTTP.Send()
HTTP.WaitForResponse()
msgbox % HTTP.ResponseText


; CreateFormData() by tmplinshi, AHK Topic: https://autohotkey.com/boards/viewtopic.php?t=7647
; Thanks to Coco: https://autohotkey.com/boards/viewtopic.php?p=41731#p41731
; Modified version by SKAN, 09/May/2016

CreateFormData(ByRef retData, ByRef retHeader, objParam) {
	New CreateFormData(retData, retHeader, objParam)
}

Class CreateFormData {

	__New(ByRef retData, ByRef retHeader, objParam) {

		Local CRLF := &quot;`r`n&quot;, i, k, v, str, pvData
		; Create a random Boundary
		Local Boundary := this.RandomBoundary()
		Local BoundaryLine := &quot;------------------------------&quot; . Boundary

    this.Len := 0 ; GMEM_ZEROINIT|GMEM_FIXED = 0x40
    this.Ptr := DllCall( &quot;GlobalAlloc&quot;, &quot;UInt&quot;,0x40, &quot;UInt&quot;,1, &quot;Ptr&quot;  )          ; allocate global memory

		; Loop input paramters
		For k, v in objParam
		{
			If IsObject(v) {
				For i, FileName in v
				{
					str := BoundaryLine . CRLF
					     . &quot;Content-Disposition: form-data; name=&quot;&quot;&quot; . k . &quot;&quot;&quot;; filename=&quot;&quot;&quot; . FileName . &quot;&quot;&quot;&quot; . CRLF
					     . &quot;Content-Type: &quot; . this.MimeType(FileName) . CRLF . CRLF
          this.StrPutUTF8( str )
          this.LoadFromFile( Filename )
          this.StrPutUTF8( CRLF )
				}
			} Else {
				str := BoundaryLine . CRLF
				     . &quot;Content-Disposition: form-data; name=&quot;&quot;&quot; . k &quot;&quot;&quot;&quot; . CRLF . CRLF
				     . v . CRLF
        this.StrPutUTF8( str )
			}
		}

		this.StrPutUTF8( BoundaryLine . &quot;--&quot; . CRLF )

    ; Create a bytearray and copy data in to it.
    retData := ComObjArray( 0x11, this.Len ) ; Create SAFEARRAY = VT_ARRAY|VT_UI1
    pvData  := NumGet( ComObjValue( retData ) + 8 + A_PtrSize )
    DllCall( &quot;RtlMoveMemory&quot;, &quot;Ptr&quot;,pvData, &quot;Ptr&quot;,this.Ptr, &quot;Ptr&quot;,this.Len )

    this.Ptr := DllCall( &quot;GlobalFree&quot;, &quot;Ptr&quot;,this.Ptr, &quot;Ptr&quot; )                   ; free global memory 

    retHeader := &quot;multipart/form-data; boundary=----------------------------&quot; . Boundary
	}

  StrPutUTF8( str ) {
    Local ReqSz := StrPut( str, &quot;utf-8&quot; ) - 1
    this.Len += ReqSz                                  ; GMEM_ZEROINIT|GMEM_MOVEABLE = 0x42
    this.Ptr := DllCall( &quot;GlobalReAlloc&quot;, &quot;Ptr&quot;,this.Ptr, &quot;UInt&quot;,this.len + 1, &quot;UInt&quot;, 0x42 )   
    StrPut( str, this.Ptr + this.len - ReqSz, ReqSz, &quot;utf-8&quot; )
  }
  
  LoadFromFile( Filename ) {
    Local objFile := FileOpen( FileName, &quot;r&quot; )
    this.Len += objFile.Length                     ; GMEM_ZEROINIT|GMEM_MOVEABLE = 0x42 
    this.Ptr := DllCall( &quot;GlobalReAlloc&quot;, &quot;Ptr&quot;,this.Ptr, &quot;UInt&quot;,this.len, &quot;UInt&quot;, 0x42 )
    objFile.RawRead( this.Ptr + this.Len - objFile.length, objFile.length )
    objFile.Close()       
  }

	RandomBoundary() {
		str := &quot;0|1|2|3|4|5|6|7|8|9|a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z&quot;
		Sort, str, D| Random
		str := StrReplace(str, &quot;|&quot;)
		Return SubStr(str, 1, 12)
	}

	MimeType(FileName) {
		n := FileOpen(FileName, &quot;r&quot;).ReadUInt()
		Return (n        = 0x474E5089) ? &quot;image/png&quot;
		     : (n        = 0x38464947) ? &quot;image/gif&quot;
		     : (n&amp;0xFFFF = 0x4D42    ) ? &quot;image/bmp&quot;
		     : (n&amp;0xFFFF = 0xD8FF    ) ? &quot;image/jpeg&quot;
		     : (n&amp;0xFFFF = 0x4949    ) ? &quot;image/tiff&quot;
		     : (n&amp;0xFFFF = 0x4D4D    ) ? &quot;image/tiff&quot;
		     : &quot;application/octet-stream&quot;
	}

}</code></pre></div>]]></content>
			<author>
				<name><![CDATA[Malcev]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=26930</uri>
			</author>
			<updated>2020-03-01T16:34:04Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=138187#p138187</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK: VK getMessagesUploadServer]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=138185#p138185" />
			<content type="html"><![CDATA[<p>Если 0, то в чем тогда выражается ошибка?</p>]]></content>
			<author>
				<name><![CDATA[Malcev]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=26930</uri>
			</author>
			<updated>2020-03-01T13:50:51Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=138185#p138185</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK: VK getMessagesUploadServer]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=138184#p138184" />
			<content type="html"><![CDATA[<p>Выводит 0 постоянно. Когда есть ошибка и когда её нет.<strong>Malcev</strong></p>]]></content>
			<author>
				<name><![CDATA[kolotilov256]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=40656</uri>
			</author>
			<updated>2020-03-01T13:48:10Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=138184#p138184</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK: VK getMessagesUploadServer]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=138182#p138182" />
			<content type="html"><![CDATA[<p>В предложении, где на конце стоит выражение в скобках, точки будут неуместны. Вы чего такой придира, модератор? Хотите поумничать - помогите с проблемой. Я даже заплатить могу.</p>]]></content>
			<author>
				<name><![CDATA[kolotilov256]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=40656</uri>
			</author>
			<updated>2020-03-01T13:37:37Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=138182#p138182</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK: VK getMessagesUploadServer]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=138168#p138168" />
			<content type="html"><![CDATA[<p><span style="color: green"><strong>kolotilov256</strong>, в конце предложения должны быть знаки препинания! Исправьте.<br />Кнопка &quot;Удалить&quot; есть у Разработчиков.</span></p>]]></content>
			<author>
				<name><![CDATA[ypppu]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=5974</uri>
			</author>
			<updated>2020-03-01T07:27:57Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=138168#p138168</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK: VK getMessagesUploadServer]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=138167#p138167" />
			<content type="html"><![CDATA[<p>Проверяйте после каждого вызова метода у ком объекта A_LastError.</p>]]></content>
			<author>
				<name><![CDATA[Malcev]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=26930</uri>
			</author>
			<updated>2020-03-01T07:22:02Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=138167#p138167</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK: VK getMessagesUploadServer]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=138164#p138164" />
			<content type="html"><![CDATA[<p>Также оставлю закреплённый файл исходник, на котором тестировал всё это<span style="color: red">.</span></p>]]></content>
			<author>
				<name><![CDATA[kolotilov256]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=40656</uri>
			</author>
			<updated>2020-03-01T02:15:48Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=138164#p138164</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[AHK: VK getMessagesUploadServer]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=138163#p138163" />
			<content type="html"><![CDATA[<p>Хочу загрузить фотографию через группу ВКонтакте в личные сообщения (используя свой id и токен группы)<span style="color: red">.</span></p><p>Столкнулся с проблемами, что у некоторых пользователей не срабатывает AHK метод getMessagesUploadServer, реализованный функцией uploadFile(url, filePath)<span style="color: red">.</span></p><p>Если кто-то действительно понимает, как решить эту дичь, можете написать в вк - <a href="http://vk.com/kolotilov1">vk.com/kolotilov1</a><span style="color: red">.</span></p><div class="codebox"><pre><code>uploadFile(url, filePath)
{
	CreateFormData(postData, hdr_ContentType, { file: [ filePath ] })

	whr := ComObjCreate(&quot;WinHttp.WinHttpRequest.5.1&quot;)
	whr.Open(&quot;POST&quot;, url, false)
	whr.SetRequestHeader(&quot;Content-Type&quot;, hdr_ContentType)
	whr.Send(postData)

	if (whr.Status != 200)
		throw Exception(whr.responseText)

	return JSON.Load(whr.responseText)
}</code></pre></div><p>P.S:</p><p>Я ещё раз повторю, что у меня всё работает, а у моего знакомого нет, проблема точно не в группе ВКонтакте или фотографии. Проблема именно в функции uploadFile(url, filePath).</p><p>Связанную библиотеку для этого метода отправил в прикреплённый файл (screenshot_api.ahk)<span style="color: red">.</span></p>]]></content>
			<author>
				<name><![CDATA[kolotilov256]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=40656</uri>
			</author>
			<updated>2020-03-01T02:14:11Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=138163#p138163</id>
		</entry>
</feed>
