定制 williamson/laragram 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

williamson/laragram

Composer 安装命令:

composer require williamson/laragram

包简介

A Laravel Wrapper for telegram-cli

README 文档

README

This project was originally forked from: php-telegram-cli-client and credit to zyberspace for the start I needed to get this project working.

What is new in 1.0?

  • To make the laravel service provider "deferred" (performance enhancing feature), you must now manually register the TG alias in the config/app.php alias section.

What is it?

This project allows you to use the lovely Laravel syntax you are familar with to quickly and easily send messages / images / documents / audio files / location via Telegram Messenger with the aid of telegram-cli running on your server.

It allows you do things like:

	TG::sendMessage('User_name', 'Hi there your account has been set up! Thanks!');

or

	TG::sendDocument('User_name', '/home/data/files/important.pdf');

or

	TG::sendImage('User_name', 'http://upload.wikimedia.org/wikipedia/commons/1/16/HDRI_Sample_Scene_Balls_(JPEG-HDR).jpg');

Of course if you don't like using Facades you can always revert to

	// $tg is an instantiated TgCommands Object. Laravel will create this out of the IOC container for
	// you if typehinted in constructor etc. 
	$tg->sendMessage('User_name', 'Hi there your account has been set up! Thanks!');

Requirements

  • A running telegram-cli listening on a unix-socket (-S) or a port (-P). Needs to be configured already (phone-number, etc.).
  • php >= 5.4.0
  • curl installed
  • json flag added to the telegram daemon startup command

Installing Telegram-cli

Setup telegram-cli

Telegram-cli needs to run on a unix-socket (-S) or a port (-P), so laragram can connect to it. Please read the instructions at telegram-cli on how to configure and then make

If your build was successful you can continue!

First time running telegram-cli

If you never started telegram-cli before, you need to start it first in normal mode, so you can type in your telegram-phone-number and register it.

Assuming you installed telegram in ~/telegram your command should look like this: ~/telegram/bin/telegram-cli.

Once registered, you can exit telegram-cli (safe_quit) and launch it as a daemon.

Running telegram-cli as a daemon

To run telegram-cli as a daemon you need to use the -d flag and set a unix socket -S.

~/telegram/bin/telegram-cli -k ~/telegram/tg-server.pub -dWS /tmp/tg.sck --json

Or instead you may use a TCP port

~/telegram/bin/telegram-cli -k ~/telegram/tg-server.pub -dWP 7777 --json

The -W switch means the contact-list gets loaded on startup - this allows you to send messages straight away. The & at the ends means that the command will load in the background allowing you to continue with the script.

To stop the daemon use killall telegram-cli or kill -TERM [telegram-pid].

You now have Telegram-cli working and waiting to be told what to send!

To ensure your daemon does not exit unexpectantly, you can use something like supervisor to make sure the process always is running. See notes at the end of how this can be set up.

Installing Laragram

Install Laragram with composer

In your laravel project-root:

composer require williamson/laragram

Service Providers and Facades

Open config/app.php file, find the providers array and add to the bottom

        'Williamson\Laragram\Laravel\LaragramServiceProvider'

If you're running Laravel 5.1 or greater you can use this syntax instead in the config/app.php file:

        Williamson\Laragram\Laravel\LaragramServiceProvider::class

Note the lack of single quotes for the laravel 5 version.

Now you add the following line to the Alias array in config/app.php to register the shorthand TG facade

        'TG' => \Williamson\Laragram\Laravel\LaragramFacade::class,

Open config/services.php file, add the following array to specify what type of socket you would like to connect to the telegram daemon.

    'telegram' => [
        'socket' =>'tcp://127.0.0.1:7778'
    ]

Other values include unix sockets. ie 'socket' => 'unix:///tmp/tg.sck'

You're done!

Using Laragram

Now anywhere in your app you can send a telegram message quickly and easily like follows:

   // routes.php

     Route::get('/test', function (){
          TG::sendMsg('<name or telegram id number>', 'Hello there!');
     });

Remember that the name should have underscores instead of spaces eg firstname_lastname, OR you can use user#and then the persons telegram ID directly. This method is far more reliable. eg user#123456789

In addition to sendMessage, the following commands are available to you:

broadcastMsg
chatAddUser
chatCreateGroup
chatCreateSecret
chatDelUser
chatExportLink
chatInfo
chatRename
chatSetPhoto
contactAdd
contactDelete
contactList
contactRename
deleteMsg
exportCard
getContactList
getDialogList
getHistory
getUserInfo
markRead
msg
sendAudio
sendContact
sendDocument
sendLocation
sendMsg
sendPhoto
sendText
sendTypingStart
sendTypingStop
sendVideo
setProfileName
setProfilePhoto
setStatusOffline
setStatusOnline
setUsername

License

This software is licensed under the Mozilla Public License v. 2.0. For more information, read the file LICENSE.

Using Supervisor

Assuming that you have

  • supervisord installed on your system (if not supervisord install ) and
  • telegram has been installed in /home/username/telegram

Then create a new file /etc/supervisor/conf.d/telegram.conf

and copy the following into it, adding/replacing any log files you wish to create

[program:telegram]
command=/bin/bash -c "rm -f /tmp/tg.sck && /home/username/telegram/bin/telegram-cli -k /home/username/telegram/tg-server.pub -dWS /tmp/tg.sck --json"
;For tcp socket this could be
;command=/bin/bash -c "/home/username/telegram/bin/telegram-cli -k /home/username/telegram/tg-server.pub -dWP 7777 --json"
directory=/home/username/telegram
redirect_stderr=true
stopsignal=KILL
stopasgroup=true
autostart=true
autorestart=true
startretries=3
user=<insert linux username>
stdout_logfile=<insert your own log folder and log filename. Must exist>
stderr_logfile=<insert your own log folder and log filename. Must exist>
stdout_logfile_maxbytes=10MB
stdout_logfile_backups=10
stdout_capture_maxbytes=1MB

Save the file and restart supervisor (make sure that telegram-cli is NOT running before you do this, supervisor will now take care of insuring that the process always runs even if it crashes).

sudo service supervisor restart

You can view how the process is doing by using the monitoring program sudo supervisorctl

williamson/laragram 适用场景与选型建议

williamson/laragram 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.75k 次下载、GitHub Stars 达 28, 最近一次更新时间为 2015 年 05 月 03 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 「php-cli」 「telegram」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 williamson/laragram 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 williamson/laragram 我们能提供哪些服务?
定制开发 / 二次开发

基于 williamson/laragram 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 2.75k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 30
  • 点击次数: 11
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 28
  • Watchers: 3
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: MPL-2.0
  • 更新时间: 2015-05-03