DEV Community

Cover image for Membuat Bot Telegram Sederhana dengan PHP dan Nutgram
superXdev
superXdev

Posted on

Membuat Bot Telegram Sederhana dengan PHP dan Nutgram

Halo teman-teman! Jika kamu tertarik membuat bot Telegram dengan PHP, yuk ikuti langkah-langkahnya 😊

Persiapan Awal

  1. Akun Telegram dan BotFather:

    • Pastikan kamu punya akun Telegram.
    • Cari BotFather di Telegram dan gunakan perintah /newbot untuk membuat bot baru. Ikuti petunjuknya dengan senang hati.
  2. Instalasi Nutgram:

    • Gunakan Composer (teman terbaik PHP) untuk menginstal Nutgram. Ketik perintah berikut:
     composer require nutgram/nutgram
    

Struktur Dasar Proyek

Buat struktur dasar proyek seperti ini:

project/
|-- vendor/
|-- composer.json
|-- index.php
Enter fullscreen mode Exit fullscreen mode

Kode PHP

1. Install Nutgram

composer require nutgram/nutgram
Enter fullscreen mode Exit fullscreen mode

2. Buat file index.php

<?php

require __DIR__ . '/vendor/autoload.php';

use SergiX44\Nutgram\Nutgram;
use SergiX44\Nutgram\Telegram\Properties\MessageType;

// Ganti TOKEN_BOT_ANDA dengan token bot yang kamu dapatkan dari BotFather
$token = 'TOKEN_BOT';

$bot = new Nutgram($token);

$bot->onCommand('start', function(Nutgram $bot) {
    $bot->sendMessage('Halo! Selamat datang 😊');
});

$bot->onText('hello', function(Nutgram $bot) {
    $bot->sendMessage('Hello, how are you? 🌈');
});

$bot->onMessageType(MessageType::PHOTO, function (Nutgram $bot) {
    $photos = $bot->message()->photo;
    $bot->sendMessage('Nice pic!');
});

$bot->run();
Enter fullscreen mode Exit fullscreen mode

3. Jalankan Bot

Jalankan bot dengan perintah:

php index.php
Enter fullscreen mode Exit fullscreen mode

Gunakan Bot Kamu

  1. Buka Telegram dan cari bot kamu.
  2. Mulai percakapan dengan bot dengan perintah /start.
  3. Coba ketikkan pesan hello dan kirim gambar.

Bot SS

Selamat! Kamu baru saja membuat bot Telegram sederhana dengan sangat mudah menggunakan Nutgram dan PHP. Silakan kembangkan lebih lanjut sesuai kreativitasmu! πŸš€

Repository: https://github.com/superXdev/telegram-bot-nutgram

Untuk informasi lebih lanjut tentang Nutgram bisa cek web dokumentasinya di https://nutgram.dev

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay