BotMan - Sending Messages (2024)

  • Introduction
  • Single Message Replies
  • Attachments
  • Type Indicators
  • Originating Messages
  • Send Low-Level Requests

Introduction

Bots send messages to deliver information and present an interface for theirfunctionality. BotMan can send messages in several different ways, dependingon the type and number of messages that will be sent.

Single message replies to incoming commands can be sent using the $bot->reply() function.

Multi-message replies, particularly those that present questions for the end user to respond to,are sent using the $bot->startConversation() function and the related conversation sub-functions.

Bots can also originate messages - that means, sending messages based on some internal logic or external stimulus - using the $bot->say() method.

Single Message Replies

Once a bot has received a message using hears(), you may send a response using $bot->reply().

This is the simplest way to respond to an incoming command:

$botman->hears('single response', function (BotMan $bot) { $bot->reply("Tell me more!");});

Try it out

Do you feel like chatting some more? You may also send multiple responses in a single method.

$botman->hears('multi response', function (BotMan $bot) { $bot->reply("Tell me more!"); $bot->reply("And even more");});

Try it out

Attachments

A common use case would be including an attachment along with your message.Depending on the messaging services that you are using, BotMan can send images, videos, audio files, generic files or even location data as attachment information.

You may do this by composing your message using the OutgoingMessage class. This class takes care of transforming your data for eachindividual messaging service.

use BotMan\BotMan\Messages\Attachments\Image;use BotMan\BotMan\Messages\Outgoing\OutgoingMessage;$botman->hears('image attachment', function (BotMan $bot) { // Create attachment $attachment = new Image('https://botman.io/img/logo.png'); // Build message object $message = OutgoingMessage::create('This is my text') ->withAttachment($attachment); // Reply message object $bot->reply($message);});

Try it out

Images

You may use the Image class to attach an image URL to your outgoing message.It takes an image URL and an optional custom payload as constructor parameters,

use BotMan\BotMan\Messages\Attachments\Image;use BotMan\BotMan\Messages\Outgoing\OutgoingMessage;// Create attachment$attachment = new Image('http://image-url-here.jpg', [ 'custom_payload' => true,]);// Build message object$message = OutgoingMessage::create('This is my text') ->withAttachment($attachment);// Reply message object$bot->reply($message);

Videos

You may use the Video class to attach a video URL to your outgoing message.It takes a video URL and an optional custom payload as constructor parameters.

use BotMan\BotMan\Messages\Attachments\Video;use BotMan\BotMan\Messages\Outgoing\OutgoingMessage;// Create attachment$attachment = new Video('http://video-url-here.mp4', [ 'custom_payload' => true,]);// Build message object$message = OutgoingMessage::create('This is my text') ->withAttachment($attachment);// Reply message object$bot->reply($message);

Audio

You may use the Audio class to attach an audio URL to your outgoing message.It takes an audio URL and an optional custom payload as constructor parameters.

use BotMan\BotMan\Messages\Attachments\Audio;use BotMan\BotMan\Messages\Outgoing\OutgoingMessage;// Create attachment$attachment = new Audio('http://audio-url-here.mp3', [ 'custom_payload' => true,]);// Build message object$message = OutgoingMessage::create('This is my text') ->withAttachment($attachment);// Reply message object$bot->reply($message);

Generic Files

You may use the File class to attach a generic file URL to your outgoing message.It takes a file URL and an optional custom payload as constructor parameters.

use BotMan\BotMan\Messages\Attachments\File;use BotMan\BotMan\Messages\Outgoing\OutgoingMessage;// Create attachment$attachment = new File('http://file-url-here.pdf', [ 'custom_payload' => true,]);// Build message object$message = OutgoingMessage::create('This is my text') ->withAttachment($attachment);// Reply message object$bot->reply($message);

Location Information

You may use the Location class to attach a GPS location information to your outgoing message.It takes the latitude, longitude and an optional custom payload as constructor parameters.

use BotMan\BotMan\Messages\Attachments\Location;use BotMan\BotMan\Messages\Outgoing\OutgoingMessage;// Create attachment$attachment = new Location(61.766130, -6.822510, [ 'custom_payload' => true,]);// Build message object$message = OutgoingMessage::create('This is my text') ->withAttachment($attachment);// Reply message object$bot->reply($message);

Type Indicators

To make your bot feel and act more human, you can make it send "typing ..." indicators.

$botman->hears('keyword', function (BotMan $bot) { $bot->typesAndWaits(2); $bot->reply("Tell me more!");});

This will send a typing indicator and sleep for 2 seconds, before actually sending the "Tell me more!" response.

Please note, that not all messaging services support typing indicators. If it is not supported, it will simply do nothing and just reply the message.

Originating Messages

BotMan also allows you to send messages to your chat users programatically. You could, for example, send out a daily message to your users that get's triggeredby your cronjob.

The easiest way is to just specify the driver-specific recipient ID when calling the say method and the driver to use.

$botman->say('Message', 'my-recipient-user-id', TelegramDriver::class);

Just as the regular reply method, this method also accepts either simple strings or OutgoingMessage objects.

For BotFramework (Skype) you should pass ['serviceUrl' => 'https://smba.trafficmanager.net/apis/'] as fourth parameter.

Sending Low-Level Requests

Sometimes you develop your chatbot and come to the conclusion that you would need to call a particular native messaging service API. As BotMan tries to decouple as many APIs as possible, it is not possible to have all API methods for each messaging service in the core of BotMan.

For this exact reason, there is the sendRequest method on the BotMan instance. What it does is, that it calls the messaging service endpoint with the given arguments and returns a Response object.

// Calling the sendSticker API for Telegram$botman->hears('sticker', function($bot) {$bot->sendRequest('sendSticker', ['sticker' => '1234'])});

If your API endpoint needs a Chat-ID, User-ID or Channel-ID that BotMan already knows, it will be passed along in the appropriate format for you, so you do not need to worry about it.

BotMan - Sending Messages (2024)
Top Articles
What Are Credit Bureaus and How Do They Work? - Experian
Protecting Source Code Intellectual Property
Cranes For Sale in United States| IronPlanet
Is Sam's Club Plus worth it? What to know about the premium warehouse membership before you sign up
Katmoie
Practical Magic 123Movies
Georgia Vehicle Registration Fees Calculator
Toyota gebraucht kaufen in tacoma_ - AutoScout24
AB Solutions Portal | Login
1TamilMV.prof: Exploring the latest in Tamil entertainment - Ninewall
Rochester Ny Missed Connections
Roblox Character Added
Becky Hudson Free
LeBron James comes out on fire, scores first 16 points for Cavaliers in Game 2 vs. Pacers
Shuiby aslam - ForeverMissed.com Online Memorials
Los Angeles Craigs List
Ts Lillydoll
Cinebarre Drink Menu
Leader Times Obituaries Liberal Ks
Everything We Know About Gladiator 2
Farmer's Almanac 2 Month Free Forecast
Vigoro Mulch Safe For Dogs
Lista trofeów | Jedi Upadły Zakon / Fallen Order - Star Wars Jedi Fallen Order - poradnik do gry | GRYOnline.pl
Schedule An Oil Change At Walmart
Ahn Waterworks Urgent Care
north jersey garage & moving sales - craigslist
Barber Gym Quantico Hours
2013 Ford Fusion Serpentine Belt Diagram
Pearson Correlation Coefficient
Wics News Springfield Il
Gas Buddy Prices Near Me Zip Code
Nottingham Forest News Now
Askhistorians Book List
Tire Pro Candler
RUB MASSAGE AUSTIN
Games R Us Dallas
Sephora Planet Hollywood
Viewfinder Mangabuddy
159R Bus Schedule Pdf
Wrigley Rooftops Promo Code
2700 Yen To Usd
Seminary.churchofjesuschrist.org
'Guys, you're just gonna have to deal with it': Ja Rule on women dominating modern rap, the lyrics he's 'ashamed' of, Ashanti, and his long-awaited comeback
The Attleboro Sun Chronicle Obituaries
Trivago Anaheim California
'The Night Agent' Star Luciane Buchanan's Dating Life Is a Mystery
Paperlessemployee/Dollartree
Tropical Smoothie Address
Bridgeport Police Blotter Today
Home | General Store and Gas Station | Cressman's General Store | California
Costco Gas Price Fort Lauderdale
Latest Posts
Article information

Author: Amb. Frankie Simonis

Last Updated:

Views: 6279

Rating: 4.6 / 5 (56 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Amb. Frankie Simonis

Birthday: 1998-02-19

Address: 64841 Delmar Isle, North Wiley, OR 74073

Phone: +17844167847676

Job: Forward IT Agent

Hobby: LARPing, Kitesurfing, Sewing, Digital arts, Sand art, Gardening, Dance

Introduction: My name is Amb. Frankie Simonis, I am a hilarious, enchanting, energetic, cooperative, innocent, cute, joyous person who loves writing and wants to share my knowledge and understanding with you.