<p>Hey,</p>
<p>New to Go, and decided to build a slack bot which interacts with a meme generator site such as imgflip to post a meme with top and bottom text specified such as '/meme badLuckBrian, top text, bottom_text'.</p>
<p>I'm fine with most of it and have individual bits working but I'm struggling with structuring the JSON correctly in my request to the Slack webhook when posting attachments - an array of hashes. So I can get it posting the a message to Slack but not an attachment (in Go that is; I have used the API successfully via Curl and Ruby).</p>
<p>See below and <a href="http://play.golang.org/p/W9A3LQ0QCf">http://play.golang.org/p/W9A3LQ0QCf</a> to see my issue.</p>
<p>Currently returning
{"text":"foo","username":"baz","channel":"bar","attachments":"[{'image_url': '<a href="http://i.imgflip.com/1bij.jpg'%7D%5D%22">http://i.imgflip.com/1bij.jpg'}]"</a>}</p>
<p>Need to return
{"text":"foo","username":"baz","channel":"bar","attachments":[{'image_url': '<a href="http://i.imgflip.com/1bij.jpg'%7D%5D">http://i.imgflip.com/1bij.jpg'}]</a>}</p>
<p>The obvious issue here is Attachment is a string, but I am unsure of what data type I should be using in the struct, or whether my approach is correct at all.</p>
<p>Like I said, new to Go and just picking up things I've seen in GitHub repos.</p>
<p>Advice appreciated. Count me as a noob.</p>
<p>Cheers,
Adam.</p>
<p>___<strong><em>EDIT</em></strong>___</p>
<p>Thanks everyone for the help. Went with the approach of treating Attachment as it's own struct as Slack Attachments can have other attributes than image_url.</p>
<p>Cheers! Adam.</p>
<hr/>**评论:**<br/><br/>mwholt: <pre><p>< shameless plug for <a href="https://mholt.github.io/json-to-go/">https://mholt.github.io/json-to-go/</a> ></p></pre>drummeradam89: <pre><p>Aha! Shameless plug accepted - this really helps. Cheers!</p></pre>alexwhoizzle: <pre><blockquote>
<p><a href="https://mholt.github.io/json-to-go/" rel="nofollow">https://mholt.github.io/json-to-go/</a></p>
</blockquote>
<p>Do you know of something like this for csv files? If not then I might try to make of port of json-to-go for csv. </p>
<p>Really liking caddy too! </p></pre>mwholt: <pre><p>Hmmm, I don't, but that's a cool idea. I recommend using <a href="http://papaparse.com" rel="nofollow">Papa Parse</a> for parsing the CSV sample. (There I go again - another shameless plug!)</p>
<p>(Thanks for your comment, by the way - glad you like it.)</p></pre>uncle_bad_touches: <pre><p>Try this: <a href="http://play.golang.org/p/cTHMaYLyhY" rel="nofollow">http://play.golang.org/p/cTHMaYLyhY</a></p></pre>jayposs: <pre><p>This solution should work as well. </p>
<pre><code>type attachment struct {
ImageUrl string `json:"image_url"`
}
type slackMessage struct {
Text string `json:"text"`
Username string `json:"username"`
Channel string `json:"channel"`
Attachments []attachment `json:"attachments"`
}
slackAttachments := make([]attachment, cnt) // cnt = # of attachments
// if you have an array (slice) of url's, use a for loop
slackAttachments = append(slackAttachments, attachment{"url1"})
slackAttachments = append(slackAttachments, attachment{"url2"})
set payload slackMessage.Attachments = slackAttachments
</code></pre></pre>jasonrichardsmith: <pre><p>Depending on your use case I would make it more expressive.
<a href="http://play.golang.org/p/K4BST_Pm2k" rel="nofollow">http://play.golang.org/p/K4BST_Pm2k</a></p></pre>drummeradam89: <pre><p>This works well for this use case as there are other attributes to an attachment that could be used. Cheers!</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传