2017-03-24 26 views
1

メッセージに添付ファイルを追加しようとすると、テキストのみが表示されるか、「エラー」というテキストが表示されない場合は、「no_text」というメッセージが表示されます。 chat.postMessageの添付ファイルですか?slack chat.postMessage添付ファイルはno_textを返す

これは私がメッセージを送信するために使用Pythonのコードです:

私は、以下の溶液を用いて行ったコメントに基づいて
r = requests.post('https://slack.com/api/chat.postMessage', params=json.loads(""" 
    { 
     "token": "xoxp-mytokenhere", 
     "channel": "C4mychannelhere", 
     "attachments": [ 
      { 
       "text": "Question?", 
       "fallback": "Question?", 
       "callback_id": "callback_id", 
       "color": "#3AA3E3", 
       "attachment_type": "default", 
       "actions": [ 
        { 
         "name": "question", 
         "text": "Yes", 
         "style": "good", 
         "type": "button", 
         "value": "yes" 
        }, 
        { 
         "name": "question", 
         "text": "Nope", 
         "style": "good", 
         "type": "button", 
         "value": "no" 
        } 
       ] 
      } 
     ] 
    } 
""")) 

:あなたが送信しようとしているように見えます

r = requests.post('https://slack.com/api/chat.postMessage',  params=json.loads({ 
     "token": "xoxp-mytokenhere", 
     "channel": "C4mychannelhere", 
     "attachments": json.dumps([ 
      { 
       "text": "Question?", 
       "fallback": "Question?", 
       "callback_id": "callback_id", 
       "color": "#3AA3E3", 
       "attachment_type": "default", 
       "actions": [ 
        { 
         "name": "question", 
         "text": "Yes", 
         "style": "good", 
         "type": "button", 
         "value": "yes" 
        }, 
        { 
         "name": "question", 
         "text": "Nope", 
         "style": "good", 
         "type": "button", 
         "value": "no" 
        } 
       ] 
      } 
     ]) 
    })) 

答えて

2

あなたの全体のセットとしてのJSON文字列をchat.postMessageに変換します。

chat.postMessageや他のウェブAPIメソッドtokenchannelattachmentsのようなあなたのフィールドが代わりにアプリケーション/ x-www-form-urlencodedでキー/値のペアとして送信されるようにのみ、URLエンコードされたクエリまたはPOSTのボディ・パラメータをサポートしています。

少し複雑になると、パラメータは実際にはになります。は、URLエンコードされたJSONデータの文字列を取ります。 JSON配列をURLエンコードし、そのパラメータに埋め込む必要があります。

あなたの目標に応じて、json.loadsを使用してスキップしてちょうどあなたのattachmentsパラメータとrequestsとしてJSON文字列をURLエンコードあなたのためにそれをの世話をすることを渡すことができますどちらか - またはあなたはネイティブのPythonにjson.dumpのようなものを使用することができます同じ属性で構築する配列

関連する問題