0

How can I let interact my Telegram Bot with Users? E.g.:

User: /buy

Bot: What do you want to buy?

User: Icecream

Bot: You have successfully bought Icecream!

So how can I do this?

switch($message) {
[...]
case "/buy":
    sendMessage($chatID, "What do you want to buy?");
    //Here I need your help :D
    break;
}
Deeonix
  • 5
  • 4

1 Answers1

1

Assuming you are using a webhook to receive updates, your php script runs again on every update you are receiving. In this case, you will need to save a certain "status" of the user which you are checking everytime your bot receives a message to indicate what you have to do next. An example would be:

switch($message) {
case "/buy":
    sendMessage($chatID, "What do you want to buy? Icecream, Water or Sprite?");
    $storage[$chatID]['status'] = 'awaitingProductChoice';
    saveStorage();
    break;
}

You need to save $storage[$userId] somehow (saveStorage();). Ideally would be to use a database, if you haven't got one, use file_put_contents('storage.json', json_encode($storage)); or something similar to serialize it. SESSIONS won't work, since Telegram Servers do not send cookies.

Then place some similar code before your switch statement:

$storage = readStorage(); // load from DB or file_get_contents from file
if ($storage[$chatID]['status'] === 'awaitingProductChoice') {
    // return " You have successfully bought Icecream!"
    $storage[$chatID]['product choice'] = $message['text'];
} else if ($storage[$chatID]['status'] === 'other stuff') {
    // other step
}
else switch($message) [...]
Jonas Fowl
  • 117
  • 5
  • Your answer is interesting, but can you please edit your answer to my edited Question? I mean: How can I e.g. save the incoming answer to a string or to an array? – Deeonix May 07 '17 at 17:10
  • @Deeonix That's simple, just save the text the user sent you. I edited my answer, maybe it helps you. – Jonas Fowl May 07 '17 at 17:28
  • And how exactly should readStorage() and saveStorage() looks like? – Deeonix May 07 '17 at 18:00
  • That depends on the way you want to read/ store your user data. E.g. if you want to use a storage.json file then it would be `function readStoreage { return json_decode(file_get_contents('storage.json'), true);}` – Jonas Fowl May 07 '17 at 18:02
  • But what is when another user send /buy to the bot? – Deeonix May 07 '17 at 18:21
  • That's why we are using the $chatID as an idex. Every user has its own ['status'] – Jonas Fowl May 07 '17 at 19:00
  • I don't get it... Can you send please a Full example code? – Deeonix May 07 '17 at 19:24
  • I have this functions and the code from above, but it doesn't work! `function saveStorage() { file_put_contents('storage.json', json_encode($storage)); } function readStorage() { return json_decode(file_get_contents('storage.json'), true); }` – Deeonix May 08 '17 at 14:30
  • Ok, you got a serious problem on a very low level. I just don't have all the time to help you out in this problem. If I find enough time later, I might send you a working demo script. Could you please upload your script to pastebin or whatever and send me the link, so I can see what you got so far? – Jonas Fowl May 08 '17 at 16:44
  • Okay, this problem extends the capacity of StackOverflow. Sorry. – Jonas Fowl May 15 '17 at 11:09