Eleirbag89 / TelegramBotPHP

A very simple PHP Telegram Bot API for sending messages.
http://eleirbag89.github.io/TelegramBotPHP
MIT License
814 stars 343 forks source link

Store text input from user #143

Closed pablosouza10 closed 6 years ago

pablosouza10 commented 6 years ago

Hi, a need this:

  1. User: input /help
  2. Bot: ask question1;
  3. User: input the answer1 text;
  4. Bot: ask question2;
  5. User: input the answer2 text;
  6. Bot: send to another group the anwser1 and answer2.

I dont know do the steps 3 and 5 to store the answer and to send next question only after the user answer previous question. Can send a example code?

emobin commented 6 years ago

define new parameter as last last command and save it in database or in a text file with the userid as the file name, and in every step read the last command for exp. ques1, ques2 , ... and send your question related to last command.

if you need more help don't hesitate to tell me.

pablosouza10 commented 6 years ago

Thanks a lot, I will try...

pablosouza10 commented 6 years ago

I need more help:

the questions are fixed like this:

user: /help bot: what your Branch? user: 15 bot: what your IP? user: 16 bot: what your Problem? user: some text...

How wait the user reply without go to another question?

the last step, I need store the users inputs to send to another group.

Mohsen322 commented 6 years ago

I have a sample code for this problem can help you Contact me on telegram @getfeedbackbot

pablosouza10 commented 6 years ago

Thanks, I use your bot with help of google translate :). Can you send me the sample code?

Best regards

pablosouza10 commented 6 years ago

Thanks @Mohsen322 help me so much!!!

emobin commented 6 years ago

i use this example code in my project, you can get some idea

`$bot_id = "Your_Token"; $telegram = new Telegram($bot_id);

// Take text and chat_id from the message $text = $telegram->Text(); $chat_id = $telegram->ChatID();

// Reply condition $replycon = $telegram->ReplyToMessageText();

if (!is_null($text) && !is_null($chat_id)) { if ($text == '/help') {

    $reply = 'question1';
    $frc = $telegram->buildForceReply(true);

    $content = ['chat_id' => $chat_id, 'reply_markup' => $frc, 'text' => $reply];
    $telegram->sendMessage($content);

} elseif ($replycon == 'question1') {
    // User Answer is stored in $text Do everything you want on $text , stor it in a text file or in Database

    InputData($chat_id ,$text);

    $reply = 'question2';
    $frc = $telegram->buildForceReply(true);

    $content = ['chat_id' => $chat_id, 'reply_markup' => $frc, 'text' => $reply];
    $telegram->sendMessage($content);

}
 elseif ($replycon == 'question2') {
    // User Answer is stored in $text Do everything you want on $text , stor it in a text file or in Database

    InputData($chat_id ,$text);

    $reply = 'Thank you to take part in this survey';
    $content = ['chat_id' => $chat_id, 'text' => $reply];
    $telegram->sendMessage($content);

    //Send your collected data to a group $group_id : the target group id

    SendGroup($chat_id ,$group_id);

} else {

$reply = 'Please answer in reply to qustion'; $content = ['chat_id' => $chat_id, 'text' => $reply]; $telegram->sendMessage($content); } }

function InputData($chatID,$data){

    $myFile = $chatID.".txt";

    $fle = fopen($myFile, 'a') or die("can't open file");
    fwrite($fle, $chatID ."\n\n");
    fclose($fle);
}

function SendGroup($chatID,$groupID){

    $myFile = $chatID.".txt";

    $fle = fopen($myFile, 'r') or die("can't open file");
    $reply = fread($fle,filesize($myFile));

    fclose($fle);

    $content = ['chat_id' => $groupID, 'text' => $reply];
    $telegram->sendMessage($content);
}`

as you see in this method, used from "forcereply" to get the exact answer for the question and go to the next question after that.

The important part is that the " $telegram->ReplyToMessageText();" object doesn't exist in default " Telegram.php" File and you need to add this object to the class as follow

`/**

I wish that this code could solve your issue, if you want more help please don't hesitate to tell me.

pablosouza10 commented 6 years ago

@emobin Thank you so much!!! Will help me sure.