Telegram Bot sends same message many times PHP - php

I am writing a telegram to the php bot, it works fine, but it sends the same message many times, it needs to be sent once, can anyone help me? here is my code
function chat_telegram( )
{
$token = <token>;
$telegram = "https://api.telegram.org/bot" . $token;
$query = http_build_query([
'chat_id' => <chat_id>,
'text' => 'I love pizza'
]);
$response = file_get_contents( $telegram . '/sendMessage?' . $query );
return $response;
}

Related

Telegram bot doesn't answer

I'm using the package of eleirbag89 http://eleirbag89.github.io/TelegramBotPHP to create my bot on telegram.
I'm having some issues on it, it doesn't answer me on telegram but the code is correct (it answered in the past and nothing has changed).
How can I debug the code to see if everything is good?
Could be a problem of SSL certificate?
This is the simple code I used to test the bot after the issue:
require '../vendor/autoload.php';
$BOT_TOKEN = "[bot:token]";
$telegram = new Telegram($BOT_TOKEN);
$result = $telegram->getData();
$text = $result['message']['text'];
$chat_id = $result['message']['chat']['id'];
if ($text == "/test") {
$msg = 'Test is good';
$content = array('chat_id' => $chat_id, 'text' => $msg);
$telegram->sendMessage($content);
}
But it doesn't answer me :(
$token = "Token";
$link01 = "https://api.telegram.org/bot".$token;
$updates = file_get_contents('php://input');
$updates = json_decode($updates, TRUE);
$msgID = $updates['message']['from']['id'];
$name = $updates['message']['from']['first_name'];
try this one.
Could be a problem of SSL certificate?
YES
You need to set webhook,
Only you can set webhook, If it is HTPS
Here is how to set web hook

How to Get Message_id from sent message By BOT Telegram?

I program the bot telegram.
I want Get message_id By Bot ,when I sent a message to Group.
My Code is PHP.
$token = "MY_BOT's_TOKEN";
$data = [
'text' => 'my message here',
'chat_id' => 'the_chat_id_here'
];
file_get_contents("https://api.telegram.org/bot$token/sendMessage?" . http_build_query($data) );
How Do it?
best regard.
Your file_get_contents call returns a result containing status code and messageId.
$result = file_get_contents("https://api.telegram.org/bot$token/sendMessage?" . http_build_query($data) );

Twitter API, posting on two accounts, only one gets postd

I have a working code that posts on twitter account via APi, using codebird. However, for some reason only the first account gets posted to, but not the second. No errors. Here's the php code:
function tweet($message) {
global $consumerkey, $consumersecret, $accesstoken, $accesssecret;
require_once('/home/curse/public_html/manage/twitter-php-sdk/src/codebird.php');
\Codebird\Codebird::setConsumerKey("$consumerkey", "$consumersecret");
$cb = \Codebird\Codebird::getInstance();
$cb->setToken("$accesstoken", "$accesssecret");
$params = array(
'status' => $message
);
$reply = $cb->statuses_update($params);
}
tweet("$twextra $makelink");
function tweet2($message) {
global $siimconsumerkey, $siimconsumersecret, $siimaccesstoken, $siimaccesssecret;
require_once('/home/curse/public_html/manage/twitter-php-sdk/src/codebird.php');
\Codebird\Codebird::setConsumerKey("$siimconsumerkey", "$siimconsumersecret");
$cb = \Codebird\Codebird::getInstance();
$cb->setToken("$siimaccesstoken", "$siimaccesssecret");
$params = array(
'status' => $message
);
$reply = $cb->statuses_update($params);
}
tweet2("$twextra $makelink");*

Grab registration data from telegram bot

I'm trying to create a Telegram API bot for clients registration , so I chose the ' setWebHook ' method and wrote some codes :
<?php
include("config.php");
$content = file_get_contents("php://input");
$update = json_decode($content, true);
$user_id = $update["message"]["chat"]["id"];
if( $update["message"]["text"] == "/start" OR $update["message"]["text"] == "/menu" ){
$keyboard = array(
'keyboard' => array(
array("📝 Register","🔑 Login")
),'one_time_keyboard'=>true,'resize_keyboard'=>true);
$replyKeyboard = json_encode($keyboard);
$replyMessage = "Hello 😊
Welcome to our bot ✋🏼
What do you want to do ?
.
";
}
if( $update["message"]["text"] == "📝 Register"){
$replyMessage = "Please enter username :";
}
$url = $bot_url.'sendMessage';
$data = array('chat_id' => $user_id,'text' => $replyMessage,'parse_mode' => 'Markdown','reply_markup' => $replyKeyboard );
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
)
);
$context = stream_context_create($options);
$update = #file_get_contents($url, false, $context);
?>
Ok so the robot shows the options for register or login , and when you choose register , it asks you username. It's ok up to here but ,
what I have to do for the rest of the registration process ??
How should I grab username and store it in database and after that grab password and store it and for other informations ?
I appreciate any logical ways for registering by telegram bots.
I wish I could completely describe my problem , I need your help my friends.
Thank you
( config.php includes database and bot token information )
As I understand from your question these are some tips maybe helpful for you:
Each message that delivers from telegram (users) have an id that tell which user sends the message(==chat_id a unique number).
Your bot should log messages and save the important parts of messages in a db. In this manner you have a track of user's conversation with your bot and you can recognize that for which question of your bot ,user answers. If you look for each Json object delivers from telegram you can see which data telegram reveals from user.

How to get reports from Commision Junction API - PHP

Commision Junction is the name of an affiliate company. I am not familiar with SOAP, WSDL and with web services in general, but wanted to quickly test the data coming back from their affiliate api. Cannot make it work though. They provide a page for their API
I tried smtg like:
public function testCJApi() {
$url = "http://" . $this->user . ":" . $this->password . "#datatransfer.cj.com/datatransfer/files/" . $this->account . "/outgoing/commission_report.csv";
$xml = simplexml_load_file($url);
if (isset($xml)) {
return ($xml
? $this->formatJsonReturn($xml, array("txt"=>"CJ Results OK","code"=>""))
: $this->formatJsonReturn("", array("txt"=>"CJ Results Empty","code"=>""))
);
}
}
but it didn't give me any results. I just need to quickly test the data coming back.
The API link they provide is
http://api.affiliatewindow.com/v4/MerchantService?wsdl.
I have figured it out myself:
public function testCJApi() {
$uri = "https://commission-detail.api.cj.com/v3/commissions?date-type=posting&start-date=2013-02-15&end-date=2013-02-17"; // can be other api uri, this is one of them
$context = stream_context_create(
array(
'http' => array(
'method' => 'GET',
'header' => 'Authorization: ' . 'YOUR API KEY GOES HERE'
)
)
);
$x = file_get_contents($uri, false, $context);
$response = new SimpleXMLElement($x);
return $response->asXML();
}

Categories