2016-10-02 121 views
0

我是Slack Bot集成的新手。我想在我的留言按鈕,所以我的代碼是Slack Bot互動消息

message = { 
     "text": "Would you like to play a game?", 
     "attachments": [ 
      { 
       "text": "Choose a game to play", 
       "attachment_type": "default", 
       "actions": [ 
        { 
         "name": "chess", 
         "text": "Chess", 
         "type": "button", 
         "value": "chess" 
        } 
       ] 
      } 
     ] 
    } 


return sc.api_call("chat.postMessage", 
        as_user="true", 
        channel=channel_id, 
        text=message) 

但在農閒通道我看到這個

text=Would+you+like+to+play+a+game%3F&attachments=%5B%7B%27text%27%3A+%27Choose+a+game+to+play%27%2C+%27attachment_type%27%3A+%27default%27%2C+%27actions%27%3A+%5B%7B%27text%27%3A+%27Chess%27%2C+%27type%27%3A+%27button%27%2C+%27name%27%3A+%27chess%27%2C+%27value%27%3A+%27chess%27%7D%5D%7D%5 

爲什麼出現這種情況?

感謝

+1

看起來這可能與您使用的鬆散API包裝有關。您是否嘗試過使用帶有'requests'的HTTP調用手動執行它?你的消息格式化[看起來很好。](https://api.slack.com/docs/messages/builder?msg=%7B%22text%22%3A%22Would%20you%20like%20to%20play%20a %20game%3F%22%2C%22attachments%22%3A%5B%7B%22text%22%3A%22Choose%20A%20game%20to%20play%22%2C%22attachment_type%22%3A%22default%22%2C %22actions%22%3A%5B%7B%22name%22%3A%22chess%22%2C%22text%22%3A%22Chess%22%2C%22type%22%3A%22button%22%2C%22value%22 %3A%22chess%22%7D%5D%7D%5D%7D) –

+0

請記住,'chat.postMessage'不接受JSON發佈主體。您需要以「x-www-form-urlencoded」參數發送您的消息。此外,'attachments'字段實際上期望JSON,但它必須是URL編碼的JSON。像這樣的純JSON消息體不能用於Web API。 –

回答

0

我沒有測試它自己,但我認爲你可以嘗試做這樣的事情:

message = "Would you like to play a game?" 
attachments = [{ 
       "text": "Choose a game to play", 
       "attachment_type": "default", 
       "actions": [ 
        { 
         "name": "chess", 
         "text": "Chess", 
         "type": "button", 
         "value": "chess" 
        } 
       ]}] 

sc.api_call("chat.postMessage", 
      channel=channel_id, 
      text=message, 
      as_user="true", 
      attachments=attachments) 

如果它不工作,我會嘗試包括「標題「字段在這樣的附件中:

attachments = [{ 
        "title": "new title", 
        ...