duanhan9334 2017-09-28 06:09
浏览 81

Telegram-bot存储相同的用户消息

I'm fighting a problem, which i can't come to an end with programming a chatbot for the company i work for. I want the User to type in /register and then the Bot asks for your EMail so he can send you a generated Token to your Mail. Instead of saving the EMail it safes /register as the EMail in my MYSQL database. I've searched the web for solutions but didn't found any that fix this problem.

I'm getting the updates via webhook.

The token and Website are set too so please don't mind that.

I Thank you for fast replies in advance.

With best regards Niklas

P.S. Sorry for my bad english I hope you understand what I want my bot to be like

<?php
    function crypto_rand_secure($min, $max)
{
    $range = $max - $min;
    if ($range < 1) return $min; // not so random...
    $log = ceil(log($range, 2));
    $bytes = (int) ($log / 8) + 1; // length in bytes
    $bits = (int) $log + 1; // length in bits
    $filter = (int) (1 << $bits) - 1; // set all lower bits to 1
    do {
        $rnd = hexdec(bin2hex(openssl_random_pseudo_bytes($bytes)));
        $rnd = $rnd & $filter; // discard irrelevant bits
    } while ($rnd > $range);
    return $min + $rnd;
}

function getToken($length)
{
    $token = "";
    $codeAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    $codeAlphabet.= "abcdefghijklmnopqrstuvwxyz";
    $codeAlphabet.= "0123456789";
    $max = strlen($codeAlphabet); // edited

    for ($i=0; $i < $length; $i++) {
        $token .= $codeAlphabet[crypto_rand_secure(0, $max-1)];
    }

    return $token;
}

$connection = mysqli_connect ('localhost','xxx','xxx','xxx','3306');
if(!$connection){
    die('Verbindung zur Datenbank konnte nicht hergestellt werden!' .mysqli_error());
}
echo 'Erfolgreich Verbunden';
$auswahl=mysqli_select_db($connection,"id3057414_testuser");
    if(!$auswahl)
    {
        die('Zugriff auf die Datenbank nicht möglich! Existiert sie wirklich?');
    }


$update = file_get_contents("php://input");
$update = json_decode($update, TRUE);

$chatID = $update["message"]["chat"]["id"];;

$text =  $update["message"]["text"];

$updateid = $update["update_id"];

$messageid = $update["message"]["message_id"]

file_get_contents($website."/sendmessage?chat_id=".$chatID."&text=$messageid!");


$lesen = "SELECT * FROM Kundenalarm";
if($text=="/register")
{


    sleep(3);
    $name = file_get_contents("php://input");
    $name = json_decode($name, TRUE);
    $name =  $name["message"]["from"]["first_name"];
    $aufnahme = mysqli_query($connection,"INSERT INTO Kundenalarm (Name,Kennung,GeräteID,Alarm) VALUES ('$name','$chatID','','')");
    if(isset($name))
    {
    file_get_contents($website."/sendmessage?chat_id=".$chatID."&text=Gib deine Email ein!");

    sleep(5);

    $email = file_get_contents("php://input");
    $email = json_decode($email, TRUE);
    $Mail= $email["message"]["text"];



    file_get_contents($website."/sendmessage?chat_id=".$chatID."&text=$Mail!");


    if(!$aufnahme)
    {
    file_get_contents($website."/sendmessage?chat_id=".$chatID."&text=Dein Name wurde nicht in die Datenbank aufgenommen !");

    }
    else
    {

        file_get_contents($website."/sendmessage?chat_id=".$chatID."&text=Dein Name wurde nun in die Datenbank aufgenommen!");
    }
    }
}

if($text=="/aktivieren")
{
    $atoken=getToken(5);
    file_get_contents($website."/sendmessage?chat_id=".$chatID."&text=$atoken");
    $result = mysqli_query($connection,"SELECT EMail FROM Kundenalarm WHERE Kennung = '$chatID'");
    $zeile = mysqli_fetch_object($result);
    $empfaenger= $zeile->EMail;
    file_get_contents($website."/sendmessage?chat_id=".$chatID."&text=$empfaenger");
    mail($empfaenger,"Aktivierungstoken Kadsoft","$atoken","Von KadsoftBot generiert!");
    file_get_contents($website."/sendmessage?chat_id=".$chatID."&text=Email gesendet!");


}

mysqli_query($connection,"SET NAMES 'utf8'");
$resultat = mysqli_query($connection,$lesen)
    or die ("Anfrage Fehlgeschlagen");

print_r($text);

print_r($chatID); 

if($text=="Hallo"||$text=="Hi")
{
    file_get_contents($website."/sendmessage?chat_id=".$chatID."&text=Hallo schön dich hier zu sehen!");

    }

if($text=="Wie ist das Wetter?")
{
    file_get_contents($website."/sendmessage?chat_id=".$chatID."&text=Heute ist es bewölkt!");
}
?>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 可见光定位matlab仿真
    • ¥15 arduino 四自由度机械臂
    • ¥15 wordpress 产品图片 GIF 没法显示
    • ¥15 求三国群英传pl国战时间的修改方法
    • ¥15 matlab代码代写,需写出详细代码,代价私
    • ¥15 ROS系统搭建请教(跨境电商用途)
    • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
    • ¥20 CST怎么把天线放在座椅环境中并仿真
    • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?
    • ¥15 YOLOv8obb获取边框坐标时报错AttributeError: 'NoneType' object has no attribute 'xywhr'