The creation of Telegram MyWeatherTodayBot

What is Telegram?

Telegram is a messaging app with a focus on speed and security, it’s super-fast, simple and free. You can use Telegram on all your devices at the same time — your messages sync seamlessly across any number of your phones, tablets or computers.

With Telegram, you can send messages, photos, videos and files of any type (doc, zip, mp3, etc), as well as create groups for up to 200,000 people or channels for broadcasting to unlimited audiences. You can write to your phone contacts and find people by their usernames. As a result, Telegram is like SMS and email combined — and can take care of all your personal or business messaging needs.

What is Telegram Bot?

Bots are third-party applications that run inside Telegram. Users can interact with bots by sending them messages, commands and inline requests.

What is MyWeatherTodayBot?

MyWeatherTodayBot is a Telegram Bot that’s able to send your location and get real time weather forecast.

How MyWeatherTodayBot made?

MyWeatherTodayBot is made by PHP and a web service to get weather forecast. First it receives user input as location (latitude and longitude), and the bot will send a REST POST to an external web service to get weather forecast.

To make your own MyWeatherTodayBot, you need Telegram Apps, and Invited @BotFather (a Telegram own bot that you can create your bot) to your accounts, that you can get started to create bot.

Step by Step to create MyWeatherTodayBot

  1. Chat with @BotFather
  2. Click and send /newbot
  3. Name your bot
  4. Copy the API token and it looks like this: 898795721:AAGkiU2Taw83m6CDpVSpH4PaVbEaFkIByHo
  5. Write a PHP file and hosts on a web server, assume the location of the file is at https://<your web server>/bot/telegrambot.php
  6. With following code
[code] $endpoint = 'https://api.telegram.org/bot898795721:AAGkiU2Taw83m6CDpVSpH4PaVbEaFkIByHo'; $action = !empty($_GET['action']) ? $_GET['action']:''; function reply($chat_id, $text, $opts = array()) { global $endpoint; $url = $endpoint . ‘/sendMessage?text=’.urlencode($text) . ‘&chat_id=’.$chat_id; if(!empty($opts[‘reply_markup’])) { $url .= ‘&reply_markup=’ . json_encode($opts[‘reply_markup’]); } if(!empty($opts[‘parse_mode’])) { $url .= ‘&parse_mode=’ . urlencode($opts[‘parse_mode’]); } file_get_contents($url); } if($action == ‘setWebhook’) { $url = ‘https://<your web server>/bot/telegrambot.php?action=incoming’; echo file_get_contents($endpoint.’/setWebhook?url=’.urlencode($url)); } else if($action == ‘info’) { echo file_get_contents($endpoint.’/getMe’); } else if($action == ‘incoming’) { $entityBody = file_get_contents(‘php://input’); $incoming = json_decode($entityBody, true); $username = $incoming[‘message’][‘from’][‘username’]; $chatid = $incoming[‘message’][‘from’][‘id’]; $input = $incoming[‘message’][‘text’]; $location = $incoming[‘message’][‘location’]; if(!empty($location)) { $latitude = $location[‘latitude’]; $longitude = $location[‘longitude’]; $userData[‘weather’][‘latitude’] = $latitude; $userData[‘weather’][‘longitude’] = $longitude; $url = “https://api.imgshow-apps.com/?api=1&k=q:name=weather,lat=$latitude,lng=$longitude,display=label,stat=telegrambot”; $label = file_get_contents($url); reply($chatid, $label); } else { reply($chatid, ‘Enter your location to get weather now’, array( ‘reply_markup’ => array( ‘one_time_keyboard’ => true, ‘keyboard’ => array( array( array(‘text’ => ‘My Location’,’request_location’ => true) ) ) ) )); } } [/code]

 

7. Now, in your Internet browser, access https:///bot/telegrambot.php?action=setWebhook, which will register a webhook to your bot.

8. And now, you can start testing with your telegram bot now.

Author: fyhao

Jebsen & Jessen Comms Singapore INTI University College Bsc (Hon) of Computer Science, Coventry University

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.