How to send images as bot responses in Slack?

Hi,

I want display images in the chat bot in slack. Can anyone please help me on how to send images from rasa to slack

Thanks in advance,

You can use urls for images or create a simple REST API for getting the image in rasa actions server using custom actions…there is a parameter image in dispatcher. dispatcher.utter_template(“utter_greet”,tracker, image=“image_url”)

Hope it helps…:slightly_smiling_face:

Hey @Bhargav, you can also create a template in your domain file that has an image like so:

  utter_cheer_up:
  - text: "Here is something to cheer you up:"
    image: "https://i.imgur.com/nGF1K8f.jpg"

then you can use the utter_cheer_up action directly or via dispatcher.utter_template("utter_cheer_up", tracker).

Hi @imabhinav-am , @erohmensing,

Thank you very much. This is working fine :slight_smile:

One small information. Can we send the images from our local path\system. I am trying to do that. Its not working :frowning_face:

For that you have to create an API on your system or host the image somewhere on internet

HI @imabhinav-am,

Can you please explain me more about how to create API?

Thanks,

@Bhargav are you able to do it with the file URI scheme? file URI scheme - Wikipedia

Depending on your OS it should look something like file:///path/to/image.png or something like that

Hi @erohmensing,

I tried the file url but this is not working

You have to use flask to create a REST Api for sending images

Hi Abhinav ? Is there any example of this on Git ? I Don’t find one :expressionless:

@Bhargav, sorry I haven’t tested myself so I can’t be sure, but if the image path doesn’t work, then you probably have to host the image somewhere.

I have to admit, I’m a bit confused about sending images as a response. Whenever I add an image to a response template, the domain won’t train/validate unless I also include text.

rasa_core.domain.InvalidDomain: Utter template ‘utter_image_reply’ needs to contain’- text: ’ attribute to be a propertemplate

What am I doing wrong?

hi @erohmensing , If I want to send multiple images how can define please help me on that, I want to create image slider in my bot UI.

you can do it with flask web server. here I have attached a sample code for that

folder_structure:

image_server:

 - images 
 - app.py

app.py

from flask import Flask, send_file
import os

app = Flask(__name__)

def get_image(image_name):
   img_dir = "./images"
   img_path = os.path.join(img_dir,image_name)
   return img_path


@app.route(’/<image_name>’,methods=[‘GET’]) 
def main(image_name): 
    image = get_image(image_name) 
    return send_file(image,mimetype=‘image/png’)

if __name__ == "__main__": 
   app.run(debug=True,port=3001)

In domain file make a response as follow:

domain.yml

utter_image_response:
 - text: sample_image
   image: localhost:3001/image_name.jpg

you need to set multiple action in your story/rule

like:

domain.yml

utter_response_1:

 - text : resposne_1
   image: url_1

utter_response_2:

 - text : resposne_2
   image: url_2

story/rule.yml

  intent: user_intent
  action: utter_response_1
  action: utter_response_2