Send message with Web API

(Chris Kyriacou) #1

Hi I’ve managed to open the bot with the web api using this code:

But what I want to do is open it, then send a message when it’s fully ready. I’ve read the docs but I’m struggling to combine the 2 (or more) commands.

It would be great if you could tell me exactly what to add to the above code to make it work.

Can you help?

(Daniel Beckett) #2

@carlito944

Hmm :thinking:

Doesn’t look like there’s any code showing for your post. Do you want to share what you’ve got so far and I’ll see if I can offer any suggestions.

(Chris Kyriacou) #3

lol, i don’t know how to display it…

(Chris Kyriacou) #4

How about a screenshot:

(Sarah Palombo) #5

Hi Chris,

Using the API are you using option 1 or 2?

Sarah :slight_smile:

(Chris Kyriacou) #6

Hmm actually neither…

I still have the code provided in my settings which I added to before the closing .

I then added the above code after . I then add the id “send_message” to any buttons or images I want to open the bot. That works fine, but now I want to take it a step further and send a message to the bot after it opens.

(Sarah Palombo) #7

When you reference the API, You’ll need to make a small change to the script tag, (depending on how you want to use the API). you should follow the example if you have a look at option 2.

You’ll see this example opens the messenger then sends a message. Hey! for instance.


// Wait until the messenger is fully
// ready, then send a message
FxoMessenger.on(‘stateChanged’, function(state) {
if (state === ‘connected’) {
FxoMessenger.sendMessage(‘Hey!’);
}
});

// Subscribe to all messages received,
// logging them to the console
FxoMessenger.on(‘messageReceived’, function(message) {
console.log(message);
});

you can then adjust your message accordingly :smiley:

Sarah

(Chris Kyriacou) #8

Thank you but I don’t understand what will trigger it to open from your example.

I want it to happen when they click an image, better still a link. In my example I see that assigning the ID makes it work. Bare in mind I have zero knowledge of JS so a piece of code I can copy an paste would be excellent.

P.s I don’t want this to happen every time, only when a specific image/link is clicked. From the example in option 2 it seems like it will happen every time…