Bot telegram dengan Yii2

ok langsung aja yaa , kita gunakan Api telegram disini https://core.telegram.org/  untuk bisa mengakses api nya, kita membutuhkan botfather layaknya CS dia akan kasih token yang sebelumnya kita membuat akun bot terlebih dulu dari botfather bisa dicek disini https://rubahmania.wordpress.com/2017/01/31/bot-telegram-dengan-foxpro/ . naa untuk di Yii saya gunakan yii2 basic, saya copas dari sini http://cintaprogramming.com/2015/07/18/membuat-bot-telegram/ terus saya sesuaikan dengan yii menggunakan httpclient dan membuat controller yang saya taruh dibawah folder commands


<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/

namespace app\commands;
use Yii;
use yii\console\Controller;
use app\models\Yiibot;
use yii\data\ActiveDataProvider;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\httpclient\Client;
/**
* This command echoes the first argument that you have entered.
*
* This command is provided as an example for you to learn how to create console commands.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class YiibotController extends Controller
{
/**
* This command echoes what you have entered as the message.
* @param string $message the message to be echoed.
*/
public function actionIndex()
{
while (true) {
$this->processOne();
}
}

protected function requesturl($method)
{
// $method = $method;
return 'https://api.telegram.org/bot'.Yii::$app->params['token'].'/'.$method;
}

protected function getUpdates($offset)
{
$url = $this->requesturl("getUpdates")."?offset=".$offset;
$resp = file_get_contents($url);
$result = json_decode($resp, true);
if ($result["ok"]==1)
return $result["result"];
return [];
}

protected function sendReply($chatid, $msgid, $text)
{
$data = [
'chat_id' => $chatid,
'text' => $text,
'reply_to_message_id' => $msgid

];

$client = new Client();
$response = $client->createRequest()
->setMethod('post')
->setUrl($this->requesturl('sendMessage'))
->addHeaders([
'content-type' => 'application/x-www-form-urlencoded',

])
->setData($data)
->send();
if ($response->isOk) {

$result= $response->content;
}

print_r($result);
}

protected function createResponse($text)
{
return "definisi " . $text;
}


protected function processMessage($message)
{
$updateid = $message["update_id"];
$message_data = $message["message"];
if (isset($message_data["text"])) {
$chatid = $message_data["chat"]["id"];
$message_id = $message_data["message_id"];
$text = $message_data["text"];
$response = $this->createresponse($text);
$this->sendreply($chatid, $message_id, $response);
}
return $updateid;
}


protected function processOne()
{
$update_id = 0;

if (file_exists("last_update_id")) {
$update_id = (int)file_get_contents("last_update_id");
}

$updates = $this->getupdates($update_id);

foreach ($updates as $message)
{
$update_id = $this->processmessage($message);
}
file_put_contents("last_update_id", $update_id + 1);
}
}

selanjutkan , buka command line (cmd) masuk kedalam folder rootnya

capture

akan jalan terus,kemudian chat deh bot telegram yang dibuat 🙂

Iklan

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: