Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 multiple values as parameters to POST command..
#1
Hi Experts,

I need some help.

What I actually need is to post some server status to slack channel with python
The below is the example code i am trying

import json
import requests

# Set the webhook_url to the one provided by Slack when you create the webhook at https://my.slack.com/services/new/incoming-webhook/
webhook_url = 'https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX'
slack_data = {'text': "Hello from python"}

response = requests.post(
    webhook_url, data=json.dumps(slack_data),
    headers={'Content-Type': 'application/json'}
)
if response.status_code != 200:
    raise ValueError(
        'Request to slack returned an error %s, the response is:\n%s'
        % (response.status_code, response.text)
    )
Though above code is working fine. But I want to pass multiple values in slack_data as above example is having only one data value is being posted on slack. But I want to mention multiple values at line no 6
I tried different methods and ended up with some errors like " too many values to unpack, Json not serializable etc

Can you some one guide me or assist me to get multiple values to be posted on slack channel

Thank you very munch in advance

best regards
ASP
Quote
#2
(Feb-19-2018, 09:20 AM)onenessboy Wrote: But I want to pass multiple values in slack_data as above example is having only one data value is being posted on slack. But I want to mention multiple values at line no 6
I tried different methods and ended up with some errors like " too many values to unpack, Json not serializable etc


Please, post the exact code you tried and full traceback of errors you get (in error tags).
It looks like you are not able to 'produce' valid json, i.e. the problem is not slack/POST related
Quote
#3
Hi Thanks for your reply,

sorry, i am replying bit late and trying to put out various methods and failing :(

The actual context is i am trying to get some linux server status and posting to slack channel

In original post I have given the sample which is base code which i am trying to build correct code for my context

This is original code
from __future__ import print_function

import boto3
import json
import logging
import os
logging.basicConfig()

from urllib2 import Request, urlopen, URLError, HTTPError

# configuration

# The Slack channel to send a message to stored in the slackChannel environment variable
SLACK_CHANNEL = "#XXXXXXXXXX"
# Add the webhook URL from Slack below
HOOK_URL = "https://hooks.slack.com/services/xxxxx/xxxx/xxxxx"

response = conn.get_all_instances()
MESSAGE = response
# Setting up logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)

slack_message = {
    'channel': SLACK_CHANNEL,
    'text': MESSAGE}

req = Request(HOOK_URL, json.dumps(slack_message))
try:
    response = urlopen(req)
    response.read()
    logger.info("Message posted to %s", slack_message['channel'])
except HTTPError as e:
    logger.error("Request failed: %d %s", e.code, e.reason)
except URLError as e:
    logger.error("Server connection failed: %s", e.reason)
So in above code line number 18 the variable "response" actually returns in below format

{
    "Reservations": [
        {
            "Instances": [
                {
                    "Monitoring": {
                        "State": "disabled"
                    },
                    "PublicDnsName": "",
                    "StateReason": {
                        "Message": "Client.UserInitiatedShutdown: User initiated shutdown",
                        "Code": "Client.UserInitiatedShutdown"
                    },
                    "State": {
                        "Code": 80,
                        "Name": "stopped"
                    },
                    "EbsOptimized": false,
                    "LaunchTime": "2018-02-08T10:37:36.000Z",
                    "PrivateIpAddress": "xx.xx.xx.xxx",
                    "ProductCodes": [],
                    "VpcId": "xxxxxx",
                    "StateTransitionReason": "User initiated (2018-02-08 11:13:34 GMT)",
                    "InstanceId": "xxxxxxx",
                    "EnaSupport": true,
                    "ImageId": "xxxxx",
                    "PrivateDnsName": "xxxxxxxx",
                    "KeyName": "xxxxxx",
                    "SecurityGroups": [
                        {
                            "GroupName": "xxxxxxxx",
                            "GroupId": "xx-xxxx"
                        }
                    ],
                    "ClientToken": "",
                    "SubnetId": "xxxx-xxxxx",
                    "InstanceType": "xx.xxx",
                    "NetworkInterfaces": [

so i want to post this information to slack channel in under stable way for readers

Hope I am giving right info.. can you please help..where i am doing it wrong...

The above code says json is not serializable error

Regargs
ASP
Quote
#4
are you sure this is full response? It is json, but not full, are you sure you receive the full response? also what is conn?
finally better use requests module, not urllib2
Quote
#5
I'm looking at boto.ec2.connection.get_all_instances() documentation
It says it returns a list of Reservations, while what you post looks like dict/json
Quote
#6
sorry i did not post entire response..

While conn is environment variable which is having connecting details

here it is
{
    "Reservations": [
        {
            "Instances": [
                {
                    "Monitoring": {
                        "State": "disabled"
                    },
                    "PublicDnsName": "",
                    "StateReason": {
                        "Message": "Client.UserInitiatedShutdown: User initiated shutdown",
                        "Code": "Client.UserInitiatedShutdown"
                    },
                    "State": {
                        "Code": 80,
                        "Name": "stopped"
                    },
                    "EbsOptimized": false,
                    "LaunchTime": "2018-02-08T10:37:36.000Z",
                    "PrivateIpAddress": "xx.xx.x.xxx",
                    "ProductCodes": [],
                    "VpcId": "xx-xxxx",
                    "StateTransitionReason": "User initiated (2018-02-08 11:13:34 GMT)",
                    "InstanceId": "xxxxxxxx",
                    "EnaSupport": true,
                    "ImageId": "xx-xxxxx",
                    "PrivateDnsName": "xxxxxxx",
                    "KeyName": "xxxxx",
                    "SecurityGroups": [
                        {
                            "GroupName": "xxxxxx",
                            "GroupId": "xxxxxxx"
                        }
                    ],
                    "ClientToken": "",
                    "SubnetId": "xxxx-xxxx",
                    "InstanceType": "t2.micro",
                    "NetworkInterfaces": [
                        {
                            "Status": "in-use",
                            "MacAddress": "xxxxxxx",
                            "SourceDestCheck": true,
                            "VpcId": "xxxxx",
                            "Description": "Primary network interface",
                            "NetworkInterfaceId": "xxxx",
                            "PrivateIpAddresses": [
                                {
                                    "PrivateDnsName": "xxxxxxx",
                                    "Primary": true,
                                    "PrivateIpAddress": "xx.xx.xx.xxx"
                                }
                            ],
                            "PrivateDnsName": "xxxxxx",
                            "Attachment": {
                                "Status": "xxx",
                                "DeviceIndex": 0,
                                "DeleteOnTermination": true,
                                "AttachmentId": "xxxxxx",
                                "AttachTime": "2017-12-28T17:24:50.000Z"
                            },
                            "Groups": [
                                {
                                    "GroupName": "xx",
                                    "GroupId": "xx-xxxx"
                                }
                            ],
                            "Ipv6Addresses": [],
                            "OwnerId": "xxxxxxx",
                            "SubnetId": "subnet-xxxxx",
                            "PrivateIpAddress": "xx.xx.x.x"
                        }
                    ],
                    "SourceDestCheck": true,
                    "Placement": {
                        "Tenancy": "default",
                        "GroupName": "",
                        "AvailabilityZone": "xx-xx-xx"
                    },
                    "Hypervisor": "xen",
                    "BlockDeviceMappings": [
                        {
                            "DeviceName": "/xx/xx",
                            "Ebs": {
                                "Status": "attached",
                                "DeleteOnTermination": true,
                                "VolumeId": "vol-xxxxxxxx",
                                "AttachTime": "2017-12-28T17:24:51.000Z"
                            }
                        }
                    ],
                    "Architecture": "x86_64",
                    "RootDeviceType": "ebs",
                    "RootDeviceName": "/dev/xvda",
                    "VirtualizationType": "hvm",
                    "Tags": [
                        {
                            "Value": "xxxx",
                            "Key": "Name"
                        }
                    ],
                    "AmiLaunchIndex": 0
                }
            ],
            "ReservationId": "00000f4510a2551",
            "Groups": [],
            "OwnerId": "xxxxxxxx"
        }
        
}
This is the traceback

Traceback (most recent call last):
File "/home/user/script/ABSlack1.py", line 43, in <module>
req = Request(HOOK_URL, json.dumps(response))
File "/usr/lib64/python2.7/json/__init__.py", line 244, in dumps
return _default_encoder.encode(obj)
File "/usr/lib64/python2.7/json/encoder.py", line 207, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib64/python2.7/json/encoder.py", line 270, in iterencode
return _iterencode(o, 0)
File "/usr/lib64/python2.7/json/encoder.py", line 184, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: Reservation:r-00000f4510a2551 "not JSON serializable
Quote
#7
note that:
1) I think you miss ] on line 108
2) I assume resp is string

try

resp = """{
    "Reservations": [
        {
            "Instances": [
                {
                    "Monitoring": {
                        "State": "disabled"
                    },
                    "PublicDnsName": "",
                    "StateReason": {
                        "Message": "Client.UserInitiatedShutdown: User initiated shutdown",
                        "Code": "Client.UserInitiatedShutdown"
                    },
                    "State": {
                        "Code": 80,
                        "Name": "stopped"
                    },
                    "EbsOptimized": false,
                    "LaunchTime": "2018-02-08T10:37:36.000Z",
                    "PrivateIpAddress": "xx.xx.x.xxx",
                    "ProductCodes": [],
                    "VpcId": "xx-xxxx",
                    "StateTransitionReason": "User initiated (2018-02-08 11:13:34 GMT)",
                    "InstanceId": "xxxxxxxx",
                    "EnaSupport": true,
                    "ImageId": "xx-xxxxx",
                    "PrivateDnsName": "xxxxxxx",
                    "KeyName": "xxxxx",
                    "SecurityGroups": [
                        {
                            "GroupName": "xxxxxx",
                            "GroupId": "xxxxxxx"
                        }
                    ],
                    "ClientToken": "",
                    "SubnetId": "xxxx-xxxx",
                    "InstanceType": "t2.micro",
                    "NetworkInterfaces": [
                        {
                            "Status": "in-use",
                            "MacAddress": "xxxxxxx",
                            "SourceDestCheck": true,
                            "VpcId": "xxxxx",
                            "Description": "Primary network interface",
                            "NetworkInterfaceId": "xxxx",
                            "PrivateIpAddresses": [
                                {
                                    "PrivateDnsName": "xxxxxxx",
                                    "Primary": true,
                                    "PrivateIpAddress": "xx.xx.xx.xxx"
                                }
                            ],
                            "PrivateDnsName": "xxxxxx",
                            "Attachment": {
                                "Status": "xxx",
                                "DeviceIndex": 0,
                                "DeleteOnTermination": true,
                                "AttachmentId": "xxxxxx",
                                "AttachTime": "2017-12-28T17:24:50.000Z"
                            },
                            "Groups": [
                                {
                                    "GroupName": "xx",
                                    "GroupId": "xx-xxxx"
                                }
                            ],
                            "Ipv6Addresses": [],
                            "OwnerId": "xxxxxxx",
                            "SubnetId": "subnet-xxxxx",
                            "PrivateIpAddress": "xx.xx.x.x"
                        }
                    ],
                    "SourceDestCheck": true,
                    "Placement": {
                        "Tenancy": "default",
                        "GroupName": "",
                        "AvailabilityZone": "xx-xx-xx"
                    },
                    "Hypervisor": "xen",
                    "BlockDeviceMappings": [
                        {
                            "DeviceName": "/xx/xx",
                            "Ebs": {
                                "Status": "attached",
                                "DeleteOnTermination": true,
                                "VolumeId": "vol-xxxxxxxx",
                                "AttachTime": "2017-12-28T17:24:51.000Z"
                            }
                        }
                    ],
                    "Architecture": "x86_64",
                    "RootDeviceType": "ebs",
                    "RootDeviceName": "/dev/xvda",
                    "VirtualizationType": "hvm",
                    "Tags": [
                        {
                            "Value": "xxxx",
                            "Key": "Name"
                        }
                    ],
                    "AmiLaunchIndex": 0
                }
            ],
            "ReservationId": "00000f4510a2551",
            "Groups": [],
            "OwnerId": "xxxxxxxx"
        }
    ]
}"""
import json
slack_msg = json.loads(resp)
slack_msg['channel'] = SLACK_CHANNEL
slack_json = json.dumps(slack_msg)
req = Request(HOOK_URL, slack_json)
again better use requests, instead of urllib2
Quote
#8
Hi,

tried as you suggested with requests.post instead of urllib2. following is the tracebak
and sorry while pasting i missed that ] which you have mentioned..

Traceback (most recent call last):
File "/home/user/script/ABSlack1.py", line 39, in <module>
slack_msg = json.loads(response)
File "/usr/lib64/python2.7/json/__init__.py", line 339, in loads
return _default_decoder.decode(s)
File "/usr/lib64/python2.7/json/decoder.py", line 364, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
TypeError: expected string or buffer

This is what code change which i tried as per you suggestions

slack_msg = json.loads(response)
slack_msg['channel'] = SLACK_CHANNEL
slack_json = json.dumps(slack_msg)
#req = Request(HOOK_URL, slack_json)
response = requests.post(
    webhook_url, data=slack_json,
    headers={'Content-Type': 'application/json'})
Quote
#9
can you check the type of response?
print(type(response))
Quote
#10
(Feb-20-2018, 02:03 PM)buran Wrote: can you check the type of response?
print(type(response))

Hi Buran,

just checked the type of response. below is the response

<class 'boto.resultset.ResultSet'>
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How to pass multiple values from one sample to nc variable? Baloch 0 95 Jun-01-2020, 09:27 PM
Last Post: Baloch
  Inserting values from multiple lists sqlite azulu 1 256 May-24-2020, 08:40 AM
Last Post: ibreeden
  UnUnloading values from multiple widgets in a container UGuntupalli 3 248 Apr-20-2020, 08:53 PM
Last Post: UGuntupalli
  Taking Multiple Command Line Argument Input bwdu 6 267 Mar-29-2020, 05:52 PM
Last Post: buran
  Save all values to pandas of multiple classes jenniferruurs 0 260 Sep-13-2019, 12:10 PM
Last Post: jenniferruurs
  sort lists of lists with multiple criteria: similar values need to be treated equal stillsen 2 522 Mar-20-2019, 08:01 PM
Last Post: stillsen
  Try for multiple values bluethundr 1 574 Mar-07-2019, 09:04 PM
Last Post: bluethundr
  Help with passing command line input to HTTP POST json sf05wrx 2 488 Feb-25-2019, 11:19 PM
Last Post: sf05wrx
  Get multiple values from function and total it. nirlep 5 784 Feb-05-2019, 06:54 AM
Last Post: nirlep
  comparing multiple values Skaperen 5 7,136 Dec-06-2017, 03:25 AM
Last Post: wavic

Forum Jump:


Users browsing this thread: 1 Guest(s)