️ 在这里查看这个库的新版本
用三行代码构建一个机器人:)
bot:: setToken ( ' token-string ' );
bot:: autoInput ();
bot:: id (chat_id)-> message ( ' hello my robot ' )-> send ();
在夜间框架中的使用
use Models botfire as bot ;
或在其他项目中的使用(查看示例)
include_once ( ' botfire .php ' );
use Models botfire as bot ;
bot:: setToken ( ' token-string ' );
bot:: autoInput ();
下载botfire并将其解压到模型目录:|
$ res =bot:: this ()-> getMe ()-> send ();
echo $ res ;
使用此方法发送短信。成功后,将返回发送的消息。
bot:: id (chat_id)
-> message ( ' text ' )
-> parse_mode (string) // HTML or Markdown
-> disable_notification (boolean)
-> reply_to (message_id)
-> keyboard ( botfire _keyboard)
-> disable_web_page_preview (boolean)
-> send ();
示例代码
bot:: id (chat_id)-> message ( ' your message text ' )-> send ();
bot:: id (chat_id)-> message ( ' use <b> parse_mode <b> ' )-> parse_mode ( ' HTML ' )-> send ();
使用其他方法
使用此方法发送照片。成功后,将返回发送的消息。
bot:: id (chat_id)
-> photo ( $ file , ' Caption ' )
-> parse_mode (string) // HTML or Markdown
-> disable_notification (boolean)
-> reply_to (message_id)
-> keyboard ( botfire _keyboard)
-> send ();
示例代码
// send from url
bot:: id (chat_id)-> photo (image_url_string)-> send ();
// send by file_id
bot:: id (chat_id)-> photo (file_id_string)-> send ();
// send file from local server
$ file =bot:: loadFile ( ' user.png ' );
bot:: id (chat_id)-> photo ( $ file )-> send ();
// send photo with caption
bot:: id (chat_id)-> photo ( $ file , ' Caption ' )-> send ();
使用此方法发送音频我们的音频必须是.MP3或.M4A格式。
bot:: id (chat_id)
-> audio ( $ file , ' Caption text for audio ' )
-> duration (int_to_seconds) //Duration of the audio in seconds
-> performer (string)
-> title (string) //Track name
-> parse_mode (string) // HTML or Markdown
-> disable_notification (boolean)
-> reply_to (message_id)
-> keyboard ( botfire _keyboard)
-> send ();
示例代码
bot:: id (chat_id)-> audio ( $ file , ' Caption text for audio ' )-> send ();
使用此方法发送一般文件
bot:: id (file_id)
-> document ( $ file , ' Caption text for document ' )
-> thumb (loadFile_or_string)
-> parse_mode (string) // HTML or Markdown
-> disable_notification (boolean)
-> reply_to (message_id)
-> keyboard ( botfire _keyboard)
-> send ();
// example
bot:: id (file_id)-> document ( file_id , ' Caption text for document ' )-> send ();
使用此方法发送视频文件
bot:: id (file_id)
-> video ( $ file , ' Caption text for video ' )
-> duration (int_to_seconds) //Duration of sent video in seconds
-> width (int) //Animation width
-> height (int) //Animation height
-> thumb (loadFile_or_string)
-> parse_mode (string) // HTML or Markdown
-> supports_streaming (boolean) //Pass True, if the uploaded video is suitable for streaming
-> disable_notification (boolean)
-> reply_to (message_id)
-> keyboard ( botfire _keyboard)
-> send ();
// example
bot:: id (file_id)-> video ( $ file , ' Caption text for video ' )-> send ();
使用此方法发送动画文件(GIF 或 H.264/MPEG-4 AVC 无声视频)。
bot:: this ()-> animation ( $ file , ' Caption text for animation ' )-> send ();
// use other methods
bot:: this ()
-> animation ( $ file , ' Caption text for animation ' )
-> duration (int_to_seconds) //Duration of sent animation in seconds
-> width (int) //Animation width
-> height (int) //Animation height
-> thumb (loadFile_or_string)
-> parse_mode (string) // HTML or Markdown
-> disable_notification (boolean)
-> reply_to (message_id)
-> keyboard ( botfire _keyboard)
-> send ();
使用此方法发送音频
bot:: this ()-> voice ( $ file , ' Caption text for voice ' )-> send ();
// use other methods
bot:: this ()
-> voice ( $ file , ' caption ' )
-> parse_mode (string) // HTML or Markdown
-> duration (int_to_seconds) //Duration of sent voice in seconds
-> disable_notification (boolean)
-> reply_to (message_id)
-> keyboard ( botfire _keyboard)
-> send ();
从 v.4.0 开始,Telegram 客户端支持最长 1 分钟的圆角方形 mp4 视频。
bot:: this ()-> voice ( video_file )-> send ();
// use other methods
bot:: this ()
-> voice ( video_file )
-> duration (int_to_seconds) //Duration of sent video in seconds
-> length (int) //Video width and height
-> thumb (loadFile_or_string)
-> disable_notification (boolean)
-> reply_to (message_id)
-> keyboard ( botfire _keyboard)
-> send ();
loadFile函数用于发送服务器文件
bot:: this ()-> document ( bot:: loadFile (file_path_string) )-> send ();
使用此方法发送地图上的点。成功后,将返回发送的消息。
bot:: this ()-> location ( $ latitude , $ longitude )-> send ();
// support live_period
bot:: this ()-> location ( $ latitude , $ longitude )-> live_period ( $ number )-> send ();
当您需要告诉用户机器人端发生了某些事情时,请使用此方法
行动清单:
'打字'、'upload_photo'、'record_video'、'upload_video'、'record_audio'、'upload_audio'、'upload_document'、'find_location'、'record_video_note'、'upload_video_note'
bot:: this ()-> chatAction ( ' typing ' )-> send ();
使用此方法可以将一组照片或视频作为相册发送。成功后,将返回已发送消息的数组。
输入媒体照片
bot:: id (chat_id)
-> mediaGroup ()
-> photo (file_id)-> caption ( ' caption ... ' )
-> photo (other_file_id)
-> send ();
// support this methods
// ..->photo(file_id)
// ->caption($caption)
// ->parse_mode('HTML')
输入媒体视频
bot:: id (chat_id)
-> mediaGroup ()
-> video (file_id)-> caption ( ' caption ... ' )
-> video (other_file_id)
-> send ();
// support this methods
// ..->video(file_id)
// ->thumb($thumb)
// ->caption($caption)
// ->parse_mode('HTML')
// ->width($width)
// ->height($height)
// ->duration($duration)
// ->supports_streaming($supports_streaming)
输入媒体动画
bot:: id (chat_id)
-> mediaGroup ()
-> animation (file_id)-> caption ( ' caption ... ' )
-> animation (other_file_id)
-> send ();
// support this methods
// ..->animation(file_id)
// ->thumb($thumb)
// ->caption($caption)
// ->parse_mode('HTML')
// ->width($width)
// ->height($height)
// ->duration($duration)
输入媒体音频
bot:: id (chat_id)
-> mediaGroup ()
-> audio (file_id)-> caption ( ' caption ... ' )
-> audio (other_file_id)
-> send ();
// support this methods
// ..->audio(file_id)
// ->thumb($thumb)
// ->caption($caption)
// ->parse_mode('HTML')
// ->duration($duration)
// ->performer($performer)
// ->title($title)
输入媒体文档
bot:: id (chat_id)
-> mediaGroup ()
-> document (file_id)-> caption ( ' caption ... ' )
-> document (other_file_id)
-> send ();
// support this methods
// ..->document(file_id)
// ->thumb($thumb)
// ->caption($caption)
// ->parse_mode('HTML')
$ k =bot:: keyboard ();
$ k -> inline ()-> row ( function ( $ col ){
// usage callback
$ col -> btn ( ' button name ' , ' callback_text ' );
// usage url
$ col -> btnUrl ( ' night framework ' , ' https://nightframework.com ' );
})
-> row ( function ( $ col ){
$ col -> btn ( ' one button ' , ' callback_text_2 ' );
});
// send message with inline button
bot:: this ()-> message ( ' message text ' )-> keyboard ( $ k )-> send ();
// send photo with inline button
bot:: this ()-> photo (file_id, ' caption ' )-> keyboard ( $ k )-> send ();
样品2
$ k =bot:: keyboard ();
$ k -> btn ( ' game 1 ' , ' game_callback ' );
$ k -> btn ( ' game 2 ' , ' game_callback ' );
$ k -> row ();
bot:: this ()-> message ( ' message text ' )-> keyboard ( $ k )-> send ();
Telegram ReplyKeyboardMarkup 文档
$ k =bot:: keyboard ();
$ k -> markup ()-> row ( function ( $ col ){
$ col -> btn ( ' button name ' );
});
$ k -> markup ( true )-> row ( function ( $ col ){
$ col -> btn ( ' button name ' );
});
bot:: this ()-> message ( ' text ' )-> keyboard ( $ k )-> send ();
bot:: this ()-> message ( ' text ' )-> removeKeyboard ()-> send ();
检查请求回调
if (bot:: $ isCallback ){
/// code ..
}
接收回调数据
$ data =bot:: get ( ' data ' );
接收callback_query_id
$ query_id = bot:: get ( ' callback_id ' );
bot:: this ()-> answerCallback ()-> send ();
// send alert
bot:: this ()-> answerCallback ( true )-> text ( ' hello Telegram :) ' )-> send ();
// or open robot link
$ link = ' t.me/your_robot?start=xxxx ' ;
bot:: this ()-> answerCallback ()-> url ( $ link )-> send ();
使用此方法可以编辑文本和游戏消息。成功后,如果机器人发送了已编辑的消息,则返回已编辑的消息,否则返回 True。
bot:: this ()-> editMessage ( ' new text string ' )-> send ();
// use other methods
bot:: this ()
-> editMessage ( ' new text string ' )
// Use custom message_id
-> message_id (id)
// Use custom inline_message_id
-> inline_message_id (id)
-> parse_mode ( ' HTML ' )
-> disable_web_page_preview (boolean)
-> keyboard ( $ k ) // A JSON-serialized object for an inline keyboard.
-> send ();
使用此方法可以编辑消息的标题。成功后,如果机器人发送了已编辑的消息,则返回已编辑的消息,否则返回 True。
bot:: this ()-> editCaption ( ' new text string ' )-> send ();
// use other methods
bot:: this ()
-> editCaption ( ' new text string ' )
// Use custom message_id
-> message_id (id)
// Use custom inline_message_id
-> inline_message_id (id)
-> parse_mode ( ' HTML ' )
-> keyboard ( $ k ) // A JSON-serialized object for an inline keyboard.
-> send ();
使用此方法仅编辑消息的回复标记。成功后,如果机器人发送了已编辑的消息,则返回已编辑的消息,否则返回 True。
bot:: this ()-> editReplyMarkup ()-> keyboard ( $ k )-> send ();
// use other methods
bot:: this ()
-> editReplyMarkup ( ' new text string ' )
// Use custom message_id
-> message_id (id)
// Use custom inline_message_id
-> inline_message_id (id)
-> keyboard ( $ k ) // A JSON-serialized object for an inline keyboard.
-> send ();
使用此方法删除消息
bot:: this ()-> deleteMessage ()-> send ();
// or
bot:: this ()-> deleteMessage ()-> message_id ( ' custom message_id ' )-> send ();
bot:: this ()-> setWebhook ( $ url )-> send ();
// or
bot:: this ()-> setWebhook ( $ url )-> max_connections ( 40 )-> send ();
// or
$ cert =bot:: loadFile ( ' certificate.txt ' );
bot:: this ()
-> setWebhook ( $ url )
-> certificate ( $ cert )
-> allowed_updates ( $ array )
-> send ();
$ res =bot:: this ()-> getWebhookInfo ()-> send ();
echo $ res ;
使用此方法获取有关聊天的最新信息(一对一对话的用户当前名称、用户、组或频道的当前用户名等)。成功时返回 Chat 对象。
$ result =bot:: this ()-> getChat ()-> send ();
// or
$ result =bot:: id (chat_id)-> getChat ()-> send ();
echo $ result ;
使用此方法获取聊天中的管理员列表。成功后,返回一个 ChatMember 对象数组,其中包含有关除其他机器人之外的所有聊天管理员的信息。如果聊天是群组或超级群组且未指定管理员,则仅返回创建者。
$ result =bot:: this ()-> getChatAdministrators ()-> send ();
// or
$ result =bot:: id (chat_id)-> getChatAdministrators ()-> send ();
echo $ result ;
使用此方法可以获取聊天中的成员数量。成功时返回 Int。
$ result =bot:: this ()-> getChatMembersCount ()-> send ();
// or
$ result =bot:: id (chat_id)-> getChatMembersCount ()-> send ();
echo $ result ;
// Get the current chat_id
$ chat_id =bot:: $ chat_id ;
// Get the current username
$ username =bot:: $ username ;
// Get the current first_name
$ first_name =bot:: $ first_name ;
// Get the current last_name
$ last_name =bot:: $ last_name ;
// first_name + last_name
$ full_name =bot:: $ full_name ;
//Receive text the user has sent
$ text =bot:: get ( ' text ' );
// get message_id
$ message_id =bot:: get ( ' message_id ' );
// get message caption
$ caption =bot:: get ( ' caption ' );
//Receive object of string sent by telegram
$ ob_str =bot:: $ input ;
//Receive json object sent by telegram
$ json ==bot:: $ json ;
检查客户端类型
// If the request is from the supergroup return true else false
$ is_group =bot:: isGroup ();
// If the request is from the supergroup or group return true else false
$ is_group =bot:: isGroup ( false );
// If the request is from the private user
$ is_user =bot:: isUser ();
获取来自超级组的请求
// get group id
$ chat_id = bot:: $ chat_id ;
// Get the group title
$ title = bot:: $ title ;
// get sender user
$ user = bot:: get ( ' user ' );
$ user_chat_id = $ user -> id ;
$ is_bot = $ user -> is_bot ;
// support messages [text,photo,video,video_note,voice,animation,document,contact,location]
$ message = bot:: getMessageType ();
输出文本示例:
{
"type" : " text " ,
"data" : " Hello botfire "
}
输出照片样本:
{
"type" : " photo " ,
"data" : [
{
"file_id" : " AgA*** " ,
"file_unique_id" : " AQA*** " ,
"file_size" : 20303 ,
"width" : 320 ,
"height" : 296
}
]
}