mirror of https://github.com/OpenIPC/firmware.git
				
				
				
			
		
			
				
	
	
		
			104 lines
		
	
	
		
			4.3 KiB
		
	
	
	
		
			Bash
		
	
	
		
			Executable File
		
	
			
		
		
	
	
			104 lines
		
	
	
		
			4.3 KiB
		
	
	
	
		
			Bash
		
	
	
		
			Executable File
		
	
| #!/bin/sh
 | |
| #
 | |
| PATH='/usr/bin:/usr/sbin:/bin:/sbin'
 | |
| 
 | |
| rupor=$1
 | |
| token=$2
 | |
| start=1
 | |
| api="https://api.telegram.org/bot$token"
 | |
| offset_file=/tmp/telegram_offset
 | |
| 
 | |
| 
 | |
| if [ "$start" == "0" ]; then
 | |
| 	echo "Launch of the Telegram_bot is not allowed." | logger -t "telegram_bot" -p daemon.info
 | |
| 	exit 1
 | |
| fi
 | |
| 
 | |
| sleep 10
 | |
| keyboard='{"keyboard": [["/snap \uD83D\uDCF7","/guard \uD83D\uDC6E","/relay \uD83D\uDCA1"],["/system \uD83D\uDCCA","/reboot \uD83D\uDCA9","/menu \uD83D\uDD25"]],"resize_keyboard":true,"one_time_keyboard":false}'
 | |
| curl -s -X POST -H "Charset: UTF-8" $api/sendMessage -d chat_id=$rupor -d parse_mode=Markdown --data-urlencode text="OpenIPC device started." >/dev/null 2>&1
 | |
| curl -s -X POST -H "Charset: UTF-8" $api/sendMessage -d chat_id=$rupor -d "reply_markup=${keyboard}" -d "text=Please insert command:" >/dev/null 2>&1
 | |
| 
 | |
| polling_timeout=30
 | |
| offset=0
 | |
| if [ -f "$offset_file" ]; then
 | |
| 	offset=$( cat $offset_file )
 | |
| else
 | |
| 	echo $offset > $offset_file
 | |
| fi
 | |
| 
 | |
| reply_to_msg () {
 | |
| 	msg_id=$1
 | |
| 	origin=$2
 | |
| 	eval toReturn="$3"
 | |
| 	curl -s -X POST -H "Charset: UTF-8" $api/sendMessage -d reply_to_message_id=$msg_id -d chat_id=$origin -d parse_mode=HTML --data-urlencode text="$toReturn" >/dev/null 2>&1
 | |
| }
 | |
| 
 | |
| while [ true ]
 | |
| do
 | |
| 	updates=$(curl -s -X GET ${api}/getUpdates?offset=${offset}&timeout=${polling_timeout})
 | |
| 	status=$(jsonfilter -s "$updates" -e $.ok)
 | |
| 	if [ $status = 'true' ]; then
 | |
| 		update_ids=$(jsonfilter -s "$updates" -e $.result[*].update_id)
 | |
| 		for update_id in $update_ids
 | |
| 		do
 | |
| 			offset=$((update_id+1))
 | |
| 			echo $offset > $offset_file
 | |
| 			origin=$(jsonfilter -s "$updates"  -e "$.result[@.update_id=$update_id].message.chat.id")
 | |
| 			msg_id=$(jsonfilter -s "$updates"  -e "$.result[@.update_id=$update_id].message.message_id")
 | |
| 			command=$(jsonfilter -s "$updates" -e "$.result[@.update_id=$update_id].message.text")
 | |
| 			is_a_cmd=$(jsonfilter -s "$updates" -e "$.result[@.update_id=$update_id].message.entities[*].type")
 | |
| 			query_ans=$(jsonfilter -s "$updates" -e "$.result[@.update_id=$update_id].callback_query.id")
 | |
| 			origin_ans=$(jsonfilter -s "$updates"  -e "$.result[@.update_id=$update_id].callback_query.message.chat.id")
 | |
| 			if [[ "$origin" != "$rupor" && "$origin_ans" != "$rupor" ]];then
 | |
| 				curl -s -X POST -H "Charset: UTF-8" $api/sendMessage -d reply_to_message_id=$msg_id -d chat_id=$origin -d parse_mode=Markdown --data-urlencode text="This is a Private bot." >/dev/null 2>&1
 | |
| 				curl -s -X POST $api/leaveChat -d chat_id=$origin >/dev/null 2>&1
 | |
| 			else
 | |
| 				if [ "$is_a_cmd" ==  "bot_command" ]; then
 | |
| 					cmd=$(echo $command |  awk '{print $1}')
 | |
| 					DATE=`date +%Y-%m-%d_%H:%M:%S`
 | |
| 					case "$cmd" in
 | |
| 						("/guard")
 | |
| 							echo "[ $DATE ] Run /guard command !" | logger -t "telegram_bot" -p daemon.info
 | |
| 							informex_guard=$("tg_guard.sh")
 | |
| 							reply_to_msg $msg_id $origin "\${informex_guard}"
 | |
| 							;;
 | |
| 						("/menu")
 | |
| 							echo "[ $DATE ] Run /menu command !" | logger -t "telegram_bot" -p daemon.info
 | |
| 							curl -s -X POST -H "Charset: UTF-8" $api/sendMessage -d chat_id=$rupor -d "reply_markup=${keyboard}" -d "text=Please insert command:" >/dev/null 2>&1
 | |
| 							;;
 | |
| 						("/reboot")
 | |
| 							echo "[ $DATE ] Run /reboot command !" | logger -t "telegram_bot" -p daemon.info
 | |
| 							informex_reboot=$("tg_reboot.sh")
 | |
| 							reply_to_msg $msg_id $origin "\${informex_reboot}"
 | |
| 							;;
 | |
| 						("/relay")
 | |
| 							echo "[ $DATE ] Run /relay command !" | logger -t "telegram_bot" -p daemon.info
 | |
| 							informex_relay=$("tg_relay.sh")
 | |
| 							reply_to_msg $msg_id $origin "\${informex_relay}"
 | |
| 							;;
 | |
| 						("/snap")
 | |
| 							echo "[ $DATE ] Run /snap command !" | logger -t "telegram_bot" -p daemon.info
 | |
| 							informex_system=$("tg_snap.sh")
 | |
| 							reply_to_msg $msg_id $origin "\${informex_system}"
 | |
| 							;;
 | |
| 						("/system")
 | |
| 							echo "[ $DATE ] Run /system command !" | logger -t "telegram_bot" -p daemon.info
 | |
| 							informex_system=$("tg_system.sh")
 | |
| 							reply_to_msg $msg_id $origin "\${informex_system}"
 | |
| 							;;
 | |
| 						(*)
 | |
| 							echo "[ $DATE ] $cmd command not enabled" | logger -t "telegram_bot" -p daemon.info
 | |
| 							informex_unknown="This command is not enabled."
 | |
| 							reply_to_msg $msg_id $origin "\${informex_unknown}"
 | |
| 							;;
 | |
| 					esac
 | |
| 				#else
 | |
| 				#	curl -s -X POST -H "Charset: UTF-8" $api/sendMessage -d reply_to_message_id=$msg_id -d chat_id=$origin -d parse_mode=Markdown --data-urlencode text="Is not a command." >/dev/null 2>&1
 | |
| 				fi
 | |
| 			fi
 | |
| 		done
 | |
| 	fi
 | |
| 	sleep 1
 | |
| done &
 |