JarvisPHPTelegramBot::run()   D
last analyzed

Complexity

Conditions 15
Paths 3

Size

Total Lines 83
Code Lines 43

Duplication

Lines 23
Ratio 27.71 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 23
loc 83
rs 4.9121
cc 15
eloc 43
nc 3
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace JarvisPHP\TelegramBot;
3
4
use JarvisPHP\TelegramBot\TelegramBotApiWrapper;
5
use JarvisPHP\TelegramBot\GenericCurl;
6
7
//Very important!
8
define('_JARVISPHP_URL','http://localhost:8000');
9
10
/**
11
 * A TelegramBot for JarvisPHP
12
 * @author Stefano Bianchini
13
 * @website http://www.stefanobianchini.net
14
 */
15
class JarvisPHPTelegramBot {
16
	
17
	public static function run($allowedClientIdList) {
18
19
		$bot = new TelegramBotApiWrapper();
20
21
		$offset = 0;
22
23
		echo "JarvisPHP Telegram Bot started at ".date('Y-m-d H:i:s')."\n";
24
		echo "-----------------------------------------------------\n";
25
26
		while(true) {
27
28
			$updates = $bot->apiRequestJson("getUpdates", array('offset'=>$offset));
29
			
30
			if($updates) {
31
				foreach($updates as $update) {
32
33
					if(isset($update->message->text)) {
34
35
						//TODO check if $update->message->chat->id is in enabled list
36
37
						$bot->apiRequestJson("sendChatAction", array('chat_id' => $update->message->chat->id, 'action' => 'typing'));
38
39
						echo "Processing message ->".$update->message->text."\n";
40
41
						$message = $update->message->text;
42
43
						$offset = $update->update_id + 1;
44
45
						$response = '';
46
47
						//Understand if the message is a telegram command (begins with "/")
48
						if(preg_match('$^/(.+)$', $message)) {
49
							
50
							//Telegram command
51
							switch($message) {
52
								case '/start': $response='I am JarvisPHP, a private bot. \u1F510'; break;
53
								case '/info': $response='I am JarvisPHP, a private bot. \u1F510'; break;
54
								case '/register': 
55
												$response='Ok, i registered your ID in registerIdLog.log'; 
56
												file_put_contents('TelegramBot/registerIdLog.log', '['.date('Y-m-d H:i:s').'] ID:'.$update->message->from->id . '; FIRSTNAME:'. $update->message->from->first_name . '; LASTNAME:'. $update->message->from->last_name. '; USERNAME:'. $update->message->from->username.PHP_EOL , FILE_APPEND | LOCK_EX);
57
												break;	
58
								case '/say': $response='Use /say "sentence" (without quotes) to make JarvisPHP speak a sentence.'; break;	
59
							}
60
61
							//"Say" Telegram command
62 View Code Duplication
							if(preg_match('$^/say (.+)$', $message, $matches)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
63
								if($matches) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $matches of type string[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
64
									if(in_array($update->message->chat->id, $allowedClientIdList)) {
65
										//Redirect message to JarvisPhp
66
										$JarvisResponse = GenericCurl::exec(_JARVISPHP_URL.'/say', array('sentence'=>$matches[1]));
67
68
										$response = $JarvisResponse->answer;
69
									} else {
70
										$response = 'You are not allowed to speak with me.';
71
									}
72
								}
73
							}
74
75
							//Encode emoji
76
							$response = preg_replace_callback('/\\\\u([0-9a-fA-F]+)/', function ($match) {
77
							    return iconv('UCS-4LE', 'UTF-8', pack('V', hexdec($match[1])));
78
							}, $response);
79
80 View Code Duplication
						} else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
81
							if(in_array($update->message->chat->id, $allowedClientIdList)) {
82
								//Redirect message to JarvisPhp
83
								$JarvisResponse = GenericCurl::exec(_JARVISPHP_URL.'/answer', array('command'=>$message, 'tts' => 'None_tts'));
84
85
								$response = $JarvisResponse->answer;
86
							} else {
87
								$response = 'You are not allowed to speak with me.';
88
							}
89
90
						}
91
						if($response) {
92
							$bot->apiRequestJson("sendMessage", array('chat_id' => $update->message->chat->id, "text" => $response));
93
						}
94
					}
95
				}
96
			}
97
			sleep(1);
98
		}
99
	}
100
}