Cara menggunakan python telegram bot html

from telegram.ext.callbackcontext import CallbackContext

from telegram.ext.commandhandler import CommandHandler

from telegram.ext.filters import Filters

from telegram.ext.messagehandler import MessageHandler

from telegram.ext.updater import Updater

from telegram.update import Update

BOT_KEY = ""

def start(update: Update, context: CallbackContext):

welcome_message = "Welcome to this bot!"

update.message.reply_text(welcome_message, parse_mode="html")

def unknown_text(update: Update, context: CallbackContext):

def _add_handlers(updater):

updater.dispatcher.add_handler(CommandHandler("start", start))

updater.dispatcher.add_handler(MessageHandler(Filters.text, unknown_text))

if __name__ == "__main__":

updater = Updater(BOT_KEY, use_context=True)

print("starting to poll...")

Artikel kali ini akan kita bahas cara mengirim pesan atau message menggunakan PHP via API Telegram ( Bot Telegram ). Bagaimana caranya? Langsung saja kita ikuti langkah-langkah berikut:

Cara membuat bot telegram sangat gampang, kalian login dulu ke aplikasi telegram kalian masing-masing untuk yang memakai android atau masuk ke web telegram https://web.telegram.org/

Langkah-langkah membuat bot telegram adalah sebagai berikut :

  1. Pada menu pencarian masukkan @botFather, kemudian silahkan klik pada akun tersebut.
    Cara menggunakan python telegram bot html
  2. Masukkan perintah /start
  3. Pilih /newbot
  4. Kemudian kita isi nama untuk bot yang kita buat
  5. Selanjutnya kita harus mengisi username untuk bot yang kita buat harus diakhiri dengan kata bot
  6. Akun boot berhasil dibuat
  7. Setelah konfirmasi bahwa akun bot berhasil dibuat, kalian akan diberi token yang digunakan untuk akses ke API telegram nantinya. Jangan lupa untuk menyimpan token tersebut di tempat yang aman.

Membuat Kode Untuk Kirim Pesan Menggunakan PHP

Untuk dokumentasi mengirim pesan (message) via bot telegram kita bisa cek di link berikut

Ada berapa parameter yang bisa digunakan untuk kirim pesan

{
  'chat_id' => '',
  'text' => '',
  'parse_mode' => 'html'
}

Keterangan :

chat_id : chat_id adalah chat_id dari user yang akan kita kirim pesan

Cara untuk mendapatkan chat_id adalah sebagai berikut.

Masuk ke menu pencarian dan ketikkan get_id_bot, klik akun tersebut dan klik mulai. Maka kita akan mendapatkan informasi chat_id akun kita

Cara menggunakan python telegram bot html

Cara menggunakan python telegram bot html

text : text adalah isi pesan yang akan kita kirim

parse_mode : parse_mode adalah opsional, jika kita ingin kirim pesan teks dengan html didalamnya kita set parse_mode ke html

Sebagai contoh kita akan buat sebuah file php bernama send.php, dan masukkan kode berikut :

";

    $string = 'bold, bold
    italic, italic
    underline, underline
    strikethrough, strikethrough, strikethrough
    spoiler, spoiler
    bold italic bold italic bold strikethrough italic bold strikethrough spoiler underline italic bold bold
    inline URL
    inline mention of a user
    inline fixed-width code
    
pre-formatted fixed-width code block
pre-formatted fixed-width code block written in the Python programming language
'; $data = [ 'chat_id' => '230965826', 'text' => $string, 'parse_mode' => "html" ]; $response = file_get_contents("https://api.telegram.org/bot$apiToken/sendMessage?" . http_build_query($data) ); echo '
';
    print_r(json_decode($response));
    die();

Untuk bisa kalian set sesuai token kalian masing-masing. Berikut ini adalah contoh response jika script di atas berhasil.

stdClass Object
(
    [ok] => 1
    [result] => stdClass Object
        (
            [message_id] => 15
            [from] => stdClass Object
                (
                    [id] => 5347100565
                    [is_bot] => 1
                    [first_name] => arietelegram
                    [username] => arietelbot
                )

            [chat] => stdClass Object
                (
                    [id] => 230965826
                    [first_name] => Iko
                    [last_name] => Handriyat Ismanto
                    [type] => private
                )

            [date] => 1653443802
            [text] => bold, bold
    italic, italic
    underline, underline
    strikethrough, strikethrough, strikethrough
    spoiler, spoiler
    bold italic bold italic bold strikethrough italic bold strikethrough spoiler underline italic bold bold
    inline URL
    inline mention of a user
    inline fixed-width code
    pre-formatted fixed-width code block
    pre-formatted fixed-width code block written in the Python programming language
            [entities] => Array
                (
                    [0] => stdClass Object
                        (
                            [offset] => 0
                            [length] => 4
                            [type] => bold
                        )

                    [1] => stdClass Object
                        (
                            [offset] => 6
                            [length] => 4
                            [type] => bold
                        )

                    [2] => stdClass Object
                        (
                            [offset] => 15
                            [length] => 6
                            [type] => italic
                        )

                    [3] => stdClass Object
                        (
                            [offset] => 23
                            [length] => 6
                            [type] => italic
                        )

                    [4] => stdClass Object
                        (
                            [offset] => 34
                            [length] => 9
                            [type] => underline
                        )

                    [5] => stdClass Object
                        (
                            [offset] => 45
                            [length] => 9
                            [type] => underline
                        )

                    [6] => stdClass Object
                        (
                            [offset] => 59
                            [length] => 13
                            [type] => strikethrough
                        )

                    [7] => stdClass Object
                        (
                            [offset] => 74
                            [length] => 13
                            [type] => strikethrough
                        )

                    [8] => stdClass Object
                        (
                            [offset] => 89
                            [length] => 13
                            [type] => strikethrough
                        )

                    [9] => stdClass Object
                        (
                            [offset] => 107
                            [length] => 7
                            [type] => spoiler
                        )

                    [10] => stdClass Object
                        (
                            [offset] => 116
                            [length] => 7
                            [type] => spoiler
                        )

                    [11] => stdClass Object
                        (
                            [offset] => 128
                            [length] => 5
                            [type] => bold
                        )

                    [12] => stdClass Object
                        (
                            [offset] => 133
                            [length] => 12
                            [type] => bold
                        )

                    [13] => stdClass Object
                        (
                            [offset] => 133
                            [length] => 12
                            [type] => italic
                        )

                    [14] => stdClass Object
                        (
                            [offset] => 145
                            [length] => 26
                            [type] => bold
                        )

                    [15] => stdClass Object
                        (
                            [offset] => 145
                            [length] => 26
                            [type] => italic
                        )

                    [16] => stdClass Object
                        (
                            [offset] => 145
                            [length] => 26
                            [type] => strikethrough
                        )

                    [17] => stdClass Object
                        (
                            [offset] => 171
                            [length] => 34
                            [type] => bold
                        )

                    [18] => stdClass Object
                        (
                            [offset] => 171
                            [length] => 34
                            [type] => italic
                        )

                    [19] => stdClass Object
                        (
                            [offset] => 171
                            [length] => 33
                            [type] => strikethrough
                        )

                    [20] => stdClass Object
                        (
                            [offset] => 171
                            [length] => 33
                            [type] => spoiler
                        )

                    [21] => stdClass Object
                        (
                            [offset] => 205
                            [length] => 26
                            [type] => bold
                        )

                    [22] => stdClass Object
                        (
                            [offset] => 205
                            [length] => 21
                            [type] => italic
                        )

                    [23] => stdClass Object
                        (
                            [offset] => 205
                            [length] => 21
                            [type] => underline
                        )

                    [24] => stdClass Object
                        (
                            [offset] => 236
                            [length] => 10
                            [type] => text_link
                            [url] => http://www.example.com/
                        )

                    [25] => stdClass Object
                        (
                            [offset] => 280
                            [length] => 23
                            [type] => code
                        )

                    [26] => stdClass Object
                        (
                            [offset] => 308
                            [length] => 36
                            [type] => pre
                        )

                    [27] => stdClass Object
                        (
                            [offset] => 349
                            [length] => 79
                            [type] => pre
                            [language] => python
                        )

                )

        )

)

Tidak Bisa Kirim Pesan Ke User Lain Via Bot telegram

Perlu diketahui, agar bisa kirim pesan ke user lain menggunakan bot telegram ada hal yang perlu dilakukan yaitu user yang akan kita kirim pesan harus terlebih dahulu pernah kirim pesan ke akun bot telegram yang kita buat. Atau kita bisa buat sebuah Group Telegram dengan menambahkan user ke dalamnya. Tapi dengan membuat group pesan (message) tidak akan dikirim personal ke akun user tersebut melainkan publish ke dalam group dimana nanti semua user bisa melihat pesan (message) tersebut.