Create modal in slack using python - python

I am using the python slack bolt. https://api.slack.com/start/building/bolt-python#create
I created the example in the link and was able to make a home tab page but I want to create a modal from a message in a channel, not a home page view. I have looked everywhere for a basic example but I can't get any MODALS to work with what I have learned from slack's own documentation. This is the only examples I can find (which came from slack's own documentation after you read the starting out page).
Here is the test example that works but shows up with a home page instead of a modal:
SLACK_BOT_TOKEN="slackbottokenstring"
SLACK_SIGNING_SECRET="slacksigningsecretstring"
import os
# Use the package we installed
from slack_bolt import App
# Initializes your app with your bot token and signing secret
app = App(
token = SLACK_BOT_TOKEN,
signing_secret = SLACK_SIGNING_SECRET
# token=os.environ.get("SLACK_BOT_TOKEN"),
# signing_secret=os.environ.get("SLACK_SIGNING_SECRET")
)
# Add functionality here
#app.event("app_home_opened")
def update_home_tab(client, event, logger):
try:
# views.publish is the method that your app uses to push a view to the Home tab
client.views_publish(
# the user that opened your app's app home
user_id=event["user"],
# the view object that appears in the app home
view={
"type": "home",
"callback_id": "home_view",
# body of the view
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*Welcome to your _App's Home_* :tada:"
}
},
{
"type": "divider"
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "This button won't do much for now but you can set up a listener for it using the `actions()` method and passing its unique `action_id`. See an example in the `examples` folder within your Bolt app."
}
},
{
"type": "actions",
"elements": [
{
"type": "button",
"text": {
"type": "plain_text",
"text": "Click me!"
}
}
]
}
]
}
)
except Exception as e:
logger.error(f"Error publishing home tab: {e}")
# Start your app
if __name__ == "__main__":
app.start(port=int(os.environ.get("PORT", 3000)))
And that works:
But if I try to change it to a modal, it fails:
CODE:
#app.event("app_home_opened")
def update_home_tab(client, event, logger):
try:
# views.publish is the method that your app uses to push a view to the Home tab
client.views_publish(
# the user that opened your app's app home
user_id=event["user"],
# the view object that appears in the app home
view={
"type": "modal",
"callback_id": "modal-identifier",
"title": {
"type": "plain_text",
"text": "Just a modal"
},
"blocks": [
{
"type": "section",
"block_id": "section-identifier",
"text": {
"type": "mrkdwn",
"text": "*Welcome* to ~my~ Block Kit _modal_!"
},
"accessory": {
"type": "button",
"text": {
"type": "plain_text",
"text": "Just a button",
},
"action_id": "button-identifier",
}
}
],
}
)
Error:
127.0.0.1 - - [14/Aug/2022 15:21:15] "POST /slack/events HTTP/1.1" 200 -
Error publishing home tab: The request to the Slack API failed. (url: https://www.slack.com/api/views.publish)
The server responded with: {'ok': False, 'error': 'invalid_arguments', 'response_metadata': {'messages': ['[ERROR] failed to match all allowed schemas [json-pointer:/view]', '[ERROR] unsupported type: modal [json-pointer:/view/type]']}}
I am following the view structure design in a json object like it says here: https://api.slack.com/surfaces/modals/using#composing_views

I figured out what I had to do but it was kinda annoying, I want to just say that first. There are a couple reasons as to why it was annoying to figure out.
There is no direct python example from the website, BUT slack does have a github where they show a modal example.
Using specifically Modals require than just making text blocks in a chat. It requires some weird thing called a trigger_id which you get from initiating a specific response that would cause slack to send an object over to your endpoint. The problem is that there is no example of this. If you are going through slack's tutorial, they ONLY show you how to create a url for making a bot that event listens and responds to messages. You can make your own url using flask but since I am using the bolt slack module, they wrap there own stuff around flask which makes it hard to make basic decorators for flask. - In other words, the slack documentation is super confusing for modals and they don't have any real examples for modals with bolt python.
after doing days of research in my spare time, I was finally able to get an example working where you can COPY AND PASTE this code. You just need your bot token secret and signing key which you can set in your environment or as a string if you are just testing around in your local computer.
SLACK_BOT_TOKEN="slackbottokenstring"
SLACK_SIGNING_SECRET="slacksigningsecretstring"
import os
# Use the package we installed
from slack_bolt import App
from slack_bolt.adapter.flask import SlackRequestHandler
from flask import Flask, request
flask_app = Flask(__name__)
# Initializes your app with your bot token and signing secret
app = App(
token = SLACK_BOT_TOKEN,
signing_secret = SLACK_SIGNING_SECRET
# token=os.environ.get("SLACK_BOT_TOKEN"),
# signing_secret=os.environ.get("SLACK_SIGNING_SECRET")
)
handler = SlackRequestHandler(app)
#flask_app.route("/slack/events", methods=["POST"])
def slack_events():
return handler.handle(request)
#app.shortcut("SearchMessagesID")
def handle_shortcuts(ack, body, logger,client):
ack()
logger.info(body)
print(body)
res = client.views_open(
trigger_id=body["trigger_id"],
view={
"type": "modal",
"callback_id": "modal-identifier",
"title": {
"type": "plain_text",
"text": "Just a modal"
},
"blocks": [
{
"type": "section",
"block_id": "section-identifier",
"text": {
"type": "mrkdwn",
"text": "*Welcome* to ~my~ Block Kit _modal_!"
},
"accessory": {
"type": "button",
"text": {
"type": "plain_text",
"text": "Just a button",
},
"action_id": "button-identifier",
}
}
],
}
)
# Start your app
if __name__ == "__main__":
app.start(port=int(os.environ.get("PORT", 3000)))

Related

error "no_text" at Slack API chat.postMessage even though text attribute is there

I tried out Slack's Bolt framework for Python. I was experimenting with the Calls API and wanted to post the call to the channel, along with some text. So I used chat.postMessage. However, I get an error ("no_text").
Below is my code (token starred out for security):
client.chat_postMessage(
token="**************",
channel="general",
blocks=[
{
"type": "call",
"call_id": slackCallId,
}
],
text="Test of Calls Api"
)
However, in the Slack channel I see this and then call:
I'm not sure why this is happening.
How about this?
[
{
"type": "slackCallId",
"call_id": "test"
},
{
"type": "section",
"text": {
"type": "plain_text",
"text": "Test of Calls Api"
}
}
]

How to send response to actions-on-google in Python?

I'm making an action-on-google assistant. I'm able to receive the request in JSON format by using Flask to establish webhook in Python. But I've no idea how to send the response back to the assistant.
enter image description here
enter image description here
import os, sys
from flask import Flask, request, send_from_directory, make_response
from googleactions import AppRequest, AppResponse, SimpleResponse
class operation():
def justPrint(self):
print("Hi dear user")
print(AppResponse('告訴我故事發生什麼事吧').json())
app = Flask(__name__)
#app.route('/', methods=['GET'])
def verify():
return "hello world"
#app.route('/', methods=['POST'])
def webhook():
req = request.get_json()
print(req)
op = operation()
getattr(op, req['handler']['name'])()
return 'ok', 200
if __name__ == "__main__":
app.run(debug=True, port=8080)
Your Flask server should return a JSON response in the correct format. It looks like you may be using the googleactions package, but unfortunately that package seems to be out-of-date with the response format expected by Actions Builder.
You should consult the JSON schema for the HandlerResponse type. As it is JSON schema, you can use a tool like Quicktype to generate the appropriate classes for additional syntax support.
The schema file also includes definitions for the internal types.
"HandlerResponse": {
"description": "Represents a response sent from a developer's fulfillment to Actions on\nGoogle.",
"type": "object",
"properties": {
"prompt": {
"description": "Optional. Represents the prompts to be sent to the user, these prompts\nwill be appended to previously added messages unless explicitly\noverwritten.",
"$ref": "#/definitions/Prompt"
},
"scene": {
"description": "Optional. Represents the current and next scene. If `Scene.next` is set\nthe runtime will immediately transition to the specified scene.",
"$ref": "#/definitions/Scene"
},
"session": {
"description": "Optional. Describes data for the current session, session\nparameters can be created, updated, or removed by the fulfillment.",
"$ref": "#/definitions/Session"
},
"user": {
"description": "Optional. Use to specify user parameters to send back.",
"$ref": "#/definitions/User"
},
"home": {
"description": "Optional. Used to specify parameters related to the HomeGraph structure\nthat the target device belongs to. See\nhttps://developers.google.com/actions/smarthome/concepts/homegraph.",
"$ref": "#/definitions/Home"
},
"device": {
"description": "Optional. Use to move between Assistant devices the user has access to.",
"$ref": "#/definitions/Device"
},
"expected": {
"description": "Optional. Describes the expectations for the next dialog turn.",
"$ref": "#/definitions/Expected"
}
}
},

Facebook Chatbot postback don't respond

I am working on fb chatbot and use postback in the generic templates buttons. However, there is no response after I click the postback button.
"buttons": [
{
"type": "web_url",
"url": "https://maps.google.com/?q={lat},{lon}".format(lat=store['coordinates']['lat'], lon=store['coordinates']['lon']),
"title": u"店家位置"
},
{
"type": "postback",
"payload": Function.sendGourmetComments + "_{id}".format(id=store["id"]),
"title": u"觀看評論"
},
{
"type": "postback",
"payload": Function.sendStoreHearts + "_{upload}".format(upload=None) + "_{id}".format(id=store["id"]) + "_{push}".format(push=None) + "_{edit}".format(edit=None) + "_{search}".format(search=True),
"title": u"撰寫評論"
}
]
I guess I didn't check the message_postback event, however, I did. I didn't even receive any response from my webhook. So it's kind of mysterious to me what's going on.
It was working fine before, but it doesn't work without change after restart my computer. I am using ngork to expose my server API as callback url.
I've this problem two days ago.
You need the page_messaging facebook's approve to receive it Facebook's approved page_messaging permission

Location widget in messenger platform displays a search bar in mobile version but not in Desktop Version

By using bot functionality provided by facebook messenger platform,
I want users to be able to provide location by using search.
It's working as expected in mobile app of the messenger as it's showing search option. But in the desktop version of the messenger, search option is not showing in location widget.
I wanted to ask that is it expected behavior or I'm missing out something.
Also in browser console, it's showing some error:
ErrorUtils caught an error:
"navigator.permissions.query(...).then(...).done is not a function".
Subsequent errors won't be logged;
see https://fburl.com/debugjs.ja # qtaMy7UNoCv.js:47.
Here's what I've tried so far:
def send_location_with_quick_reply(self, recipient_id, message, quick_replies):
url = self.get_message_url()
payload = {
"recipient":{
"id":recipient_id
},
"message":{
"text": message,
"quick_replies":[{
"content_type": "location"
}]
}
}
# _do_post will hit the send_message API of `Messenger`.
return self._do_post(url, payload)
And here's the response I'm getting after the user chooses the location:
{
"object": "page",
"entry": [{
"id": "128922990987384",
"time": 1505890084176,
"messaging": [{
"sender": {
"id": "1456347437763847"
},
"recipient": {
"id": "128922990987384"
},
"timestamp": 1505890084065,
"message": {
"mid": "mid.$cAAAvskrTvY9kz1Bs4Vengsjpb9L_",
"seq": 2366,
"attachments": [{
"title": "User's Location",
"url": "https:\\/\\/l.facebook.com\\/l.php?u=https\\u00253A\\u00252F\\u00252Fwww.bing.com\\u00252Fmaps\\u00252Fdefault.aspx\\u00253Fv\\u00253D2\\u002526pc\\u00253DFACEBK\\u002526mid\\u00253D8100\\u002526where1\\u00253D12.9703749\\u0025252C\\u00252B77.6361206\\u002526FORM\\u00253DFBKPL1\\u002526mkt\\u00253Den-US&h=ATNsjbke0tPFGIFpCq4MA5l1W6wmiwp0cTfUZNXSSbMDHxygEM4GrVlZmtaebsN4elliFhMSJNmIFwQgn-p_fxnF2hW0VdKTj2z_0hsWnH4dlLZGdQ&s=1&enc=AZN9DwrutbtXSfRAdxQf4bzFSMSO3zujAb0LBOgUt9mz16ZnDn7CSZDBLmnISjfAMbLG6b6H6hn9a3KCb6wOo7dn",
"type": "location",
"payload": {
"coordinates": {
"lat": 12.9703749,
"long": 77.6361206
}
}
}]
}
}]
}]
}
I am using python and drf to integrate with messenger platform.
Yes, this is expected behavior currently.

Ember serializer customization for alternate url

I am a newbie to ember. So here is what i faced. I got backend RESTAPI written in python/Django. It provides following json response on /api/works/
[
{
"id": 17,
"title": "about us",
"description": "some project",
"owner": "admin"
},
{
"id": 19,
"title": "test1 profit project",
"description": "dev null",
"owner": "test1"
}
]
also on detail view E.g:/api/works/17/:
{
"id": 17,
"title": "about us",
"description": "some project",
"owner": "admin"
}
there is also /api/works/17/tasks/ for listing work tasks
[
{
"id": 2,
"title": "secondWorkTask",
"description": "task2 description",
"due_date": "2016-09-26",
"assign": 1,
"project": "about us"
},
{
"id": 3,
"title": "some task name",
"description": "some task description",
"due_date": "2016-08-27",
"assign": 2,
"project": "about us"
}
]
on the front-end side i am using ember-cli version2.7.0 + ember-django-adapter. I can get /api/works without problem.
serializer on ember to get project:
export default DRFSerializer.extend({
normalizeFindAllResponse(store, primaryModelClass, payload, id, requestType) {
payload.data = payload;
return this._super(...arguments);
}
});
What i want to achieve is on the ember side when work detail url on the ember side(emberexample-app.com/app/17/) load, it must show all tasks. Currently i can get work detail by this url /api/works/17/ with above serializer. But how can i get tasks? Please help me to find a way to solve this.
The serializer are used to customize the loading and saving (or serialization and deserialization) of data.
To customize the URLs you must use an Adapter,(e.g. RESTAdapter is my most used adapter).
That will work in the case you want to create (urlForCreateRecord) or update (urlForUpdateRecord) tasks but it may not directly work if you just want to convert a work.get('tasks') following a belongsTo relationship to a GET http://endpoint/work/<work_id>/tasks, at least in my case it didn't "just work"ed.
A solution I found that works very well is adding a property links as a plain object to the parent objects that contains links to the different child models you want to retrieve as properties.
Something like this:
/* app/adapters/work.js */
import DS from 'ember';
export default DS.RESTSerializer({
normalize (model, hash, prop) {
hash.links = hash.links || {};
hash.links.tasks = this.buildURL(model, hash.id) + '/tasks';
return this._super(model, hash, prop);
}
});
In this example, when Ember try to get a tasks property from a work model, it will make a GET to the URL that work.links.tasks contains.

Categories

Resources