I'm trying to make confirmation slack button while running slash command. e.g. I run slash-command (/test) it sends the POST request to my python app which sends back 2 buttons (Confirm, Cancel). User presses one of the buttons slack triggers an action and sends another POST request to my app. All is working fine before this step - I cannot handle the data from 2nd POST request correctly.
In the Slack documentation I found this:
When an action is invoked, a request will be sent to the app's Request URL as configured above; the request body will contain a payload parameter that your app should parse for JSON.
When I do
data=json.loads(request.form["payload"])
return jsonify(data)
I get
{
"action_ts": "XXXX",
"actions": [
{
"name": "confirm",
"type": "button",
"value": "confirm"
}
],
"attachment_id": "X",
"callback_id": "XXXX",
"channel": {
"id": "XXXX",
"name": "XXXX"
},
"is_app_unfurl": false,
"message_ts": "XXXX",
"response_url": "XXXX",
"team": {
"domain": "XXXX",
"id": "XXXX"
},
"token": "XXXX",
"trigger_id": "XXXX",
"type": "interactive_message",
"user": {
"id": "XXXX",
"name": "XXXX"
}
}
After when I call
data=json.loads(request.form["payload"])
action=data["actions"]
return jsonify(action)
I get
[
{
"name": "confirm",
"type": "button",
"value": "confirm"
}
]
Now when I'm trying to get value of "name" with action["name"] I receive the error
TypeError: list indices must be integers or slices, not str
I tried json.dumps(action) and json.dumps(action["name"]) neither of them worked.
How to read that values? I need to check value of name and then do the rest with it.
[
{
"name": "confirm",
"type": "button",
"value": "confirm"
}
]
is a list containing one element - the dictionary. Access the name like data["actions"][0]["name"]
Related
I want to create two telegram bots on one adress https://example.com
this is working with webhooks, but i can't identify from which bot request came.
This is what i recieve from bot 1:
{
"update_id": 610779399,
"message": {
"message_id": 58,
"from": {
"id": 299470913,
"is_bot": false,
"first_name": "my_first_name",
"last_name": "my_last_name",
"username": "my_user_name",
"language_code": "en"
},
"chat": {
"id": 299470913,
"first_name": "my_first_name",
"last_name": "my_last_name",
"username": "my_user_name",
"type": "private"
},
"date": 1664720338,
"text": "this message is from bot 1"
}
}
This is what i recieve from Bot 2:
{
"update_id": 66917787,
"message": {
"message_id": 37910,
"from": {
"id": 299470913,
"is_bot": false,
"first_name": "my_first_name",
"last_name": "my_last_name",
"username": "my_user_name",
"language_code": "en"
},
"chat": {
"id": 299470913,
"first_name": "my_first_name",
"last_name": "my_last_name",
"username": "my_user_name",
"type": "private"
},
"date": 1664720333,
"text": "this is from bot 2"
}
}
Responses looks identical, is it even possible to identify from which bot request came?
When setting up the webhook using setWebhook API method, you can set secret_token parameter, which will specify value of X-Telegram-Bot-Api-Secret-Token header in every request sent to your webhook.
Secret token is usually used for security purposes, to make sure that the requests sent to your webhook are really coming from Telegram (and not from some malicious actor). If you use different secret tokens for two different bots, you can also use these tokens to specify from which bot the request was sent.
attached an example AVRO-Schema
{
"type": "record",
"name": "DummySampleAvroValue",
"namespace": "de.company.dummydomain",
"fields": [
{
"name": "ID",
"type": "int"
},
{
"name": "NAME",
"type": [
"null",
"string"
]
},
{
"name": "STATE",
"type": "int"
},
{
"name": "TIMESTAMP",
"type": [
"null",
"string"
]
}
]
}
Regarding the section "JSON Encoding" of the official AVRO-Specs - see: https://avro.apache.org/docs/current/spec.html#json_encoding - a JSON Message which validates against the above AVRO-Schema should look like the following because of the UNION-Types used:
{
"ID":1,
"NAME":{
"string":"Kafka"
},
"STATE":-1,
"TIMESTAMP":{
"string":"2022-04-28T10:57:03.048413"
}
}
When producing this message via Confluent Rest Proxy (AVRO), everything works fine, the data is accepted, validated and present in Kafka.
When using the "SearializingProducer" from the confluent_kafka Python Package, the example message is not accepted and only "regular" JSON works, e. g.:
{
"ID":1,
"NAME":"Kafka",
"STATE":-1,
"TIMESTAMP":"2022-04-28T10:57:03.048413"
}
Is this intended behaviour or am I doing something wrong? Can I tell the SerializingProducer to accept this encoding?
I need to hold open both ways to produce messages but the sending system can/want´s only to provide one of the above Payloads. Is there a way to use both with the same payload?
Thanks in advance.
Best regards
I would like to add a share button for social media. I've got the Slack workflow buttons working, but I'd like those receiving the message payload to be able to share what they're receiving. Here's what I've got so far.
def post_summary_to_slack(self):
data = str(
{'text': self.summary_printout,
'attachments': [
{
"fallback": "Was this a good use of time and money?",
"title": "Was this a good use of time and money?",
"callback_id": "meetings_survey",
"color": "#800080",
"attachment_type": "default",
"actions": [
{
"name": "yes",
"text": "Yes",
"type": "button",
"value": "yes"
},
{
"name": "no",
"text": "No",
"type": "button",
"value": "no"
},
{
"name": "maybe",
"text": "I'm Not Sure",
"type": "button",
"value": "maybe"
},
/* maybe here:*/
{
"name": "twitter",
"text": "Tweet",
"type": "button",
"value": "Here are the results from the latest analysis"
}
]
}
]
}
)
url = self.SLACK_HOOK
req = urllib2.Request(url, data, {'Content-Type': 'application/json'})
f = urllib2.urlopen(req)
f.close()
Has anybody tried something like this? Thanks for the help!
Sure you can do that. However, Slack does not provide a Twitter share button out-of-the-box, so your script will need to implement that functionality. So your Twitter share button will call your script (like any other message button) and your script will need to forward the message contents to Twitter by calling the correct Twitter APIs etc.
I am in the process of migrating my Parse push notification data to CleverTap. The problem that I am having is that the documentation is not very good about it and the examples that they give don't cover push notifications.
Anyone have an example of how to send pushes with CleverTap API using Python or at least evidence that this is possible?
The documentation now covers how to migrate your Parse installations to CleverTap. For example, say we are creating a push notification to be sent to users subscribed to the "yankees" and "mets" channels.
curl -H "Content-Type:application/json"
-H "X-CleverTap-Account-ID: Your CleverTap Account ID"
-H "X-CleverTap-Passcode: Your CleverTap Account Passcode"
--data "#payload"
"https://api.clevertap.com/1/targets/create.json"
Payload
"name": "My API Campaign",
"where": {
"common_profile_prop": {
"profile_fields": [
{
"name": "channels",
"value": ["yankees", "mets"]
}
]
}
},
"content":{
"title": "Breaking News",
"body": "Yankees Win 5-4",
"platform_specific": {
"ios": {
"deep_link": "example.com",
"sound_file": "example.caf",
"category": "reactive",
"badge_count": 1,
"key": "value_ios"
},
"android": {
"background_image": "http://example.jpg",
"default_sound": true,
"deep_link": "example.com",
"key": "value_android"
}
}
},
"devices": [
"android",
"ios",
],
"when": "now"
}
Response
{
"status": "success",
"id": 1457433898,
"estimates": {
"android": 90,
"ios": 300
}
}
Hope this helps. More documentation can be found here -
https://github.com/CleverTap/parse-migrate
I am new to freebase. Might be a simple answer but I cant seem to understand the freebase API. I try to run a simple query using the command queryResult = freebase.mqlread(queryFB); and I get the error below. I haven't logged in to the API. I created an account which is a google account. My question is when I call freebase.login(username,password); what do I use? Google account details? I am also attaching my query.
Query:
{
"type": "/food/dish",
"id": dish,
"name": None,
"cuisine":
[{
"id": None,
"name": None,
"region_of_origin":
[{
"id": None,
"name": None,
"/location/location/contains":
[{
"id": None,
"name": city,
"type": "/location/citytown",
"limit": 20
}]
}],
"optional": "optional"
}],
"type_of_dish1":
[{
"id": None,
"name": None,
"optional": "optional"
}]
}
Error:
{
"error":
{
"errors":
[
{
"domain": "usageLimits",
"reason": "userRateLimitExceededUnreg",
"message": "User Rate Limit Exceeded. Please sign up",
"extendedHelp": "https://code.google.com/apis/console"
}
],
"code": 403,
"message": "User Rate Limit Exceeded. Please sign up"
}
}
Sounds like you might be using an obsolete client library and/or API. The current API doesn't use a username/password pair, but rather an API key.
Try this. It will explain how to use api key for running MQL. You will need to get only the apikey for freebase. NO username or password is required.
https://developers.google.com/freebase/v1/mql-overview