Skip to content

Instantly share code, notes, and snippets.

@djnotes
Created March 25, 2023 07:31
Show Gist options
  • Save djnotes/d841adb5e464798a0358b7e40feb6a4f to your computer and use it in GitHub Desktop.
Save djnotes/d841adb5e464798a0358b7e40feb6a4f to your computer and use it in GitHub Desktop.
<?php
//Use this file to create a simple bot/userbot that receives message and
// channel updates and notifies the specified admin about it
use danog\MadelineProto\EventHandler;
use danog\MadelineProto\Tools;
use danog\MadelineProto\API;
use danog\MadelineProto\Logger;
use danog\MadelineProto\Settings;
use danog\MadelineProto\RPCErrorException;
if (!file_exists('madeline.php')) {
copy('https://phar.madelineproto.xyz/madeline.php', 'madeline.php');
}
require_once 'madeline.php';
// $MadelineProto = new \danog\MadelineProto\API('session.madeline');
// $MadelineProto->start();
// $me = $MadelineProto->getSelf();
// $MadelineProto->logger($me);
/**
* Event handler class.
*/
class MyEventHandler extends EventHandler
{
/**
* @var int|string Username or ID of bot admin
*/
const ADMIN = "REPLACE_WITH_YOUR_ID"; // Change this
/**
* Get peer(s) where to report errors
*
* @return int|string|array
*/
public function getReportPeers()
{
return [self::ADMIN];
}
/**
* Called on startup, can contain async calls for initialization of the bot
*/
public function onStart()
{
}
/**
* Handle updates from supergroups and channels
*
* @param array $update Update
*/
public function onUpdateNewChannelMessage(array $update): \Generator
{
// return $this->onUpdateNewMessage($update);
yield $this->logger("New Channel Update: " . \json_encode($update, JSON_PRETTY_PRINT));
yield $this->messages->sendMessage(peer : self::ADMIN, message : "You have a message from channel: " . $update['message']['message']);
}
/**
* Handle updates from users.
*
* @param array $update Update
*
* @return \Generator
*/
public function onUpdateNewMessage(array $update): \Generator
{
yield $this->logger("New Message Update: " . \json_encode($update, JSON_PRETTY_PRINT));
if ($update['message']['_'] === 'messageEmpty' || $update['message']['out'] ?? false) {
return;
}
//Deky add your custom handling code here
yield $this->messages->sendMessage(peer : self::ADMIN, message : "You have a message: " . $update['message']['message'], parse_mode: 'HTML');
}
}
$settings = new Settings;
$settings->getLogger()->setLevel(Logger::LEVEL_ULTRA_VERBOSE);
// For users or bots
MyEventHandler::startAndLoop('session.madeline', $settings);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment