Telegram BOT

Un bot est un système de réponse automatique qui répond à vos messages en fonction de leur contenu, exemple.

Des exemples de BOT :

Avec la librairie (Wrapper) : python-telegram-bot : (https://github.com/python-telegram-bot)

sudo apt-get install python3-venv

Comment créer un BOT en Python : https://www.commentcoder.com/bot-telegram/

cyrgui@CyrguiUbuntu:~$ python3 -m venv TelegramBOT
cyrgui@CyrguiUbuntu:~$ source TelegramBOT/bin/activate
(TelegramBOT) cyrgui@CyrguiUbuntu:~$pip install python-telegram-bot
(TelegramBOT) cyrgui@CyrguiUbuntu:~$ python bot.py &

https://medium.com/analytics-vidhya/python-telegram-bot-with-scheduled-tasks-932edd61c534

https://stackoverflow.com/questions/48238445/how-can-i-get-live-location-in-telegram-bot-via-python

ou https://www.commentcoder.com/bot-telegram/

Exemples :

https://github.com/python-telegram-bot/python-telegram-bot/blob/v13.x/examples/echobot.py

=> Exécution : sudo python3 testbot.py

Liste des processus Python : ps -aux | grep python

Tuer le processus en tache de fond : sudo kill <<numero du processus>>

S’envoyer des messages Telegram en Python :

1°) A l’aide de BotFather créer un Bot et récupérer le Token

2°) sous Python :

sudo pip3 install telegram-send

sudo telegram-send –configure

Insérez le Token

3°) Code en Python :

import telegram_send
telegram_send.send(messages=["Wow that was easy!"])

4°) Exécutez :

sudo python3 sendmsg.py

Pour stoker et retrouver la localisation :

https://github.com/daaazzzeeed/FindMyCarTelegramBot

5°) Exécution du BOT en tâche de fond et au démarrage :

Pour exécuter le BOT en tâche de fond : sudo xxx.py &

Pour relancer le bot au démarrage du serveur, copiez le fichier py du bot dans /bin et éditer le fichier cron :

sudo crontab -e

puis ajouter en fin de fichier :

@reboot sudo python3 /bin/botmeteo.py &

6°) Programmer des messages périodiquement ou à heure fixe avec UPDATER :

Utilisez REDIS cf.

https://medium.com/analytics-vidhya/python-telegram-bot-with-scheduled-tasks-932edd61c534

https://github.com/python-telegram-bot/python-telegram-bot/wiki/Extensions-%E2%80%93-JobQueue

Un exemple est donné ici

Avec une autre librairie (Wrapper) pyTelegramBotAPI:

https://betterprogramming.pub/how-to-create-telegram-bot-in-python-cccc4babcc30

https://github.com/irevenko/info-bot

Une librairie Python utile pour la météo : https://pypi.org/project/pyowm/

Une autre pour les appels d’API : https://fr.python-requests.org/en/latest/

Pour automatiser l’envoi de message : https://github.com/python-telegram-bot/python-telegram-bot/wiki/Extensions-%E2%80%93-JobQueue et https://medium.com/analytics-vidhya/python-telegram-bot-with-scheduled-tasks-932edd61c534

Code exemple ici

Pour envoyer des messages à telegram en PHP :

Simple :

function SendToTelegram($texte)
{
	
	$apiToken = "xxxxx";
	$data = [
		'chat_id' => 'xxxxx', //Utilisateur
		'text' => $texte
	];
	$response = file_get_contents("https://api.telegram.org/bot$apiToken/sendMessage?" . http_build_query($data));
}

Sans Https :

function XSendToTelegram($texte)
{	
		//////////////////////////////////////
	$opts = array(
    'ssl' => array(
        'verify_peer' => false,
        'verify_peername' => false
        // Instead ideally use
        // 'cafile' => 'path Certificate Authority file on local filesystem'
    ),
    'http' => array(
        'method' => 'POST',
        'header' => 
            'Content-type: application/x-www-form-urlencoded'."\r\n".'',
        'content' => $postdata
    )
  );
  $context = stream_context_create($opts);

	$apiToken = "xxxx";
	$data = [
		'chat_id' => 'xxxxx', //Utilisateur
		'text' => $texte
	];
	//////////

	$response = file_get_contents("https://api.telegram.org/bot$apiToken/sendMessage?" . http_build_query($data),false, $context);
}

Avec cURL :

{

  $botToken="xxxx";

  $website="https://api.telegram.org/bot".$botToken;
  $chatId=xxxx; 
  $params=[
      'chat_id'=>$chatId, 
      'text'=>$texte,
  ];
  $ch = curl_init($website . '/sendMessage');
  curl_setopt($ch, CURLOPT_HEADER, false);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, ($params));
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  $result = curl_exec($ch);
  curl_close($ch);
}

Références :

https://medium.com/@robertbracco1/how-to-write-a-telegram-bot-to-send-messages-with-python-bcdf45d0a580

Articles récents
Commentaires récents
fatima dans Bienvenue !
AdminDroid dans Bienvenue !
fatima dans Bienvenue !
Archives
Catégories