Back

Technologies:

javascriptjavascript
avatar
Tolerim
25 days ago

The error message "client.chat.sendTyping is not a function" occurs when attempting to use the Slack bolt library.

My slack bolt app operates on socket mode and listens to messages with the following code:

app.message(/.*/, async ({ message, say, context, client, ack })=>{}
Now, I want to add a 'typing...' notification feature on slack after a user types a message and before the bot responds. Although, executing this command:

await client.chat.sendTyping({
  token: context.botToken,
  channel: message.channel
});
returns an error: TypeError: client.chat.sendTyping is not a function. Despite consulting the slack documentation, I couldn't find a solution.

Could you please inform me how to implement the 'typing...' notification feature on slack for a slack bolt app operating in socket mode?

Answers(2)
avatar
Vogi
24 days ago
Verified Answer
To implement the 'typing...' notification feature on Slack for a Slack Bolt app operating in socket mode, you can use the user_typing event. This event is sent to all members of a channel when a user is typing a message in that channel. You can use this event to send a typing indicator to the channel using the chat.postMessage method with the typing parameter set to true. Here is an example of how you can use this event:
app.event('user_typing', async ({ event, client }) => {
  try {
    await client.chat.postMessage({
      channel: event.channel,
      typing: true,
    });
  } catch (error) {
    console.error(error);
  }
});
You can find more information about the user_typing event in the Slack API documentation. I hope this helps! Let me know if you have any other questions.

Sources

Responding to app mentionsHow can I make it look like a bot is typing in slack?Intro to Socket ModeConfigure your Slack notificationsuser_typing event | Slack
avatar
Tolerim
25 days ago
The client.chat.sendTyping() function is not available in Socket Mode because it is technically a Client method. In Socket Mode, you will need to manually send the typing message to the Slack API and then handle the response yourself. Here's an example of how to notify the user that the bot is 'typing...' in Socket Mode:
app.message(/.*/, async ({ message, say, context, client, ack }) => {
  try {
    // Notify the user that the bot is typing...
    await app.client.sendTypingIndicator({
      token: context.botToken,
      channel: message.channel
    });

    // Wait for a few seconds to simulate typing...
    await new Promise(resolve => setTimeout(resolve, 3000));

    // Send the actual message to the user
    await say('Hello, world!');

  } catch (error) {
    console.error(error);
  }
});
In this example, we're using the app.client.sendTypingIndicator() function to manually send the typing message to the Slack API. We're then using a setTimeout() function to wait for a few seconds to simulate typing before sending the actual message to the user with the say() function. You can adjust the setTimeout() delay to change the typing speed. Remember to import the app instance correctly for Socket Mode.
;