Skip to content

Installation (webhook)

bafplus edited this page Dec 28, 2017 · 4 revisions

Installation

Important! Before getting started with this project, make sure you have read the official readme to understand how the PHP Telegram Bot library works and what is required to run a Telegram bot.

This installation manual is based on installing the bot on Ubuntu 16.04. There are 2 methods, webhook and as service. The choice is yours. Below you wil find the differance between the 2. This page is about setting up the bot for webhook use, if you want to use the service see this wikipage -> (todo)

Webhook: Lets the bot run its script each time a new post is done in a telegram chat. This requires the bot to be available by a public url and with valid ssl encryption. Your scripts wil remain dormant if no messages are send making it light-weight for your server since its only active when it needs to. Plus side is this wil also work on (shared)webhosting.

As service: This wil run a script on your server that checks for new messages in a continuous loop. This doesen't need a public url or ssl. Because the script is always running this wil be a higher impact on system resources, yet not heavily. If you want to run the bot as service check this (todo) wiki page.

Let's get started then! 😃

Preparing the environment

For the bot to work, a standard webserver is needed. For this manual we used a Ubuntu 16.04 server with Apache and MySQL setup as usual.

For a general installation, google is your friend ;-) or use this tutorial.

If not already installed you can install by the following command:

$ sudo apt-get install apache2 mysql-server php libapache2-mod-php php-mcrypt php-mysql

0. Cloning this repository

Navigate to the root of the webserver:

cd /var/www/html/

Create a subfolder for your bot, use the bot name for easy referance, mybot in this example:

mkdir mybot

cd into the folder:

cd mybot

Git clone the repo:

git clone https://github.com/bafplus/basic-php-telegram-bot.git

Creating the TG bot

Abviously we need a Telegram bot endpoint for our script to interact with. This process is quite simple:

Open a pm with @BotFather in Telegram.

  • Say: /newbot
  • Botfather wil ask for a name for your bot. This can be any name, just make it functional.
  • Next give it a username (@username). This must ALWAYS end with _bot . Make it short and simple. Make note of the username for later use.
  • Botfather wil reply with a message after creating including the Api-key. Make note of this key, you wil need it later.

Next we wil configure the bot some more.

  • Say: /mybots
  • This wil list all your bots, click on the name of the bot you want to configure (the one you made in the prior step)
  • Click on "Bot Settings"
  • Click on "Privacy Mode"
  • Click on "turn off" (this wil allow the bot to "see" all messages in chat)

Click on "Back to settings" and "back to bot" to get back to the main settings.

  • Click on "Edit bot" and adjust the things you like, like profile picture etc.

Thats it, thats all you need to configure in Telegram. Use the above steps if you want to change or recover the api-key for example.

1. Making it yours

Now you can choose what installation you would like, either the default one or using the Bot Manager project. Depending on which one you choose, you can delete the files that are not required.


Default Next, edit the following files, replacing all necessary values with those of your project. Thanks to reading the main readme file, you should know what these do.

  • composer.json (Describes your project and it's dependencies)
  • set.php (Used to set the webhook)
  • unset.php (Used to unset the webhook)
  • hook.php (Used for the webhook method)
  • getUpdatesCLI.php (Used for the getUpdates method)
  • cron.php (Used to execute commands via cron)

Bot Manager Using the bot manager makes life much easier, as all configuration goes into a single file, manager.php.

If you decide to use the Bot Manager, be sure to read all about it and change the require block in the composer.json file:

"require": {
    "php-telegram-bot/telegram-bot-manager": "*"
}

Then, edit the following files, replacing all necessary values with those of your project.

  • composer.json (Describes your project and it's dependencies)
  • manager.php (Used as the main entry point for everything)

Now you can install all dependencies using composer:

$ composer install

To be continued!