Skip to content

Commit f84502c

Browse files
author
Nagacharan.K
committed
added Share To telegram
1 parent 7526cf9 commit f84502c

File tree

7 files changed

+132
-30
lines changed

7 files changed

+132
-30
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,13 @@
1-
connection.project.dir=../example/android
1+
arguments=
2+
auto.sync=false
3+
build.scans.enabled=false
4+
connection.gradle.distribution=GRADLE_DISTRIBUTION(WRAPPER)
5+
connection.project.dir=
26
eclipse.preferences.version=1
7+
gradle.user.home=
8+
java.home=C\:/Program Files/Eclipse Foundation/jdk-11.0.12.7-hotspot
9+
jvm.arguments=
10+
offline.mode=false
11+
override.workspace.settings=true
12+
show.console.view=true
13+
show.executions.view=true

android/src/main/java/zhuoyuan/li/fluttershareme/FlutterShareMePlugin.java

+29-1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public class FlutterShareMePlugin implements MethodCallHandler, FlutterPlugin, A
4848
final private static String _methodTwitter = "twitter_share";
4949
final private static String _methodSystemShare = "system_share";
5050
final private static String _methodInstagramShare = "instagram_share";
51+
final private static String _methodTelegramShare = "telegram_share";
5152

5253

5354
private Activity activity;
@@ -124,6 +125,10 @@ public void onMethodCall(MethodCall call, @NonNull Result result) {
124125
msg = call.argument("url");
125126
shareInstagramStory(msg, result);
126127
break;
128+
case _methodTelegramShare:
129+
msg = call.argument("msg");
130+
shareToTelegram(msg, result);
131+
break;
127132
default:
128133
result.notImplemented();
129134
break;
@@ -242,7 +247,30 @@ private void shareWhatsApp(String imagePath, String msg, Result result, boolean
242247
result.error("error", var9.toString(), "");
243248
}
244249
}
245-
250+
/**
251+
* share to telegram
252+
*
253+
* @param msg String
254+
* @param result Result
255+
*/
256+
257+
private void shareToTelegram(String msg, Result result) {
258+
try {
259+
String message = call.argument("msg");
260+
Intent telegramIntent = new Intent(Intent.ACTION_SEND);
261+
telegramIntent.setType("text/plain");
262+
telegramIntent.setPackage("org.telegram.messenger");
263+
telegramIntent.putExtra(Intent.EXTRA_TEXT, message);
264+
try {
265+
startActivity(telegramIntent);
266+
result.success("true");
267+
} catch (Exception ex) {
268+
result.success("false");
269+
}
270+
} catch (Exception var9) {
271+
result.error("error", var9.toString(), "");
272+
}
273+
}
246274
/**
247275
* share whatsapp message to personal number
248276
*
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,13 @@
1+
arguments=
2+
auto.sync=false
3+
build.scans.enabled=false
4+
connection.gradle.distribution=GRADLE_DISTRIBUTION(VERSION(7.0-rc-1))
15
connection.project.dir=
26
eclipse.preferences.version=1
7+
gradle.user.home=
8+
java.home=C\:/Program Files/Eclipse Foundation/jdk-11.0.12.7-hotspot
9+
jvm.arguments=
10+
offline.mode=false
11+
override.workspace.settings=true
12+
show.console.view=true
13+
show.executions.view=true

example/lib/main.dart

+11-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ enum Share {
1212
whatsapp_personal,
1313
whatsapp_business,
1414
share_system,
15-
share_instagram
15+
share_instagram,
16+
share_telegram
1617
}
1718

1819
void main() => runApp(MyApp());
@@ -64,6 +65,10 @@ class _MyAppState extends State<MyApp> {
6465
onPressed: () => onButtonTap(Share.share_instagram),
6566
child: const Text('share to Instagram'),
6667
),
68+
ElevatedButton(
69+
onPressed: () => onButtonTap(Share.share_telegram),
70+
child: const Text('share to Telegram'),
71+
),
6772
ElevatedButton(
6873
onPressed: () => onButtonTap(Share.share_system),
6974
child: const Text('share to System'),
@@ -110,7 +115,8 @@ class _MyAppState extends State<MyApp> {
110115
case Share.whatsapp:
111116
if (file != null) {
112117
response = await flutterShareMe.shareToWhatsApp(
113-
imagePath: file!.path, fileType: videoEnable ? FileType.video : FileType.image);
118+
imagePath: file!.path,
119+
fileType: videoEnable ? FileType.video : FileType.image);
114120
} else {
115121
response = await flutterShareMe.shareToWhatsApp(msg: msg);
116122
}
@@ -128,6 +134,9 @@ class _MyAppState extends State<MyApp> {
128134
case Share.share_instagram:
129135
response = await flutterShareMe.shareToInstagram(imagePath: file!.path);
130136
break;
137+
case Share.share_telegram:
138+
response = await flutterShareMe.shareToTelegram(msg: msg);
139+
break;
131140
}
132141
debugPrint(response);
133142
}

ios/Classes/SwiftFlutterShareMePlugin.swift

+24-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class SwiftFlutterShareMePlugin: NSObject, FlutterPlugin, SharingDelegate
1212
let _methodTwitter = "twitter_share";
1313
let _methodInstagram = "instagram_share";
1414
let _methodSystemShare = "system_share";
15+
let _methodTelegramShare = "telegram_share";
1516

1617
var result: FlutterResult?
1718
var documentInteractionController: UIDocumentInteractionController?
@@ -55,7 +56,6 @@ public class SwiftFlutterShareMePlugin: NSObject, FlutterPlugin, SharingDelegate
5556
}
5657
else if(call.method.elementsEqual(_methodWhatsAppPersonal)){
5758
let args = call.arguments as? Dictionary<String,Any>
58-
5959
shareWhatsAppPersonal(message: args!["msg"]as! String, phoneNumber: args!["phoneNumber"]as! String, result: result)
6060
}
6161
else if(call.method.elementsEqual(_methodFaceBook)){
@@ -70,6 +70,10 @@ public class SwiftFlutterShareMePlugin: NSObject, FlutterPlugin, SharingDelegate
7070
let args = call.arguments as? Dictionary<String,Any>
7171
shareInstagram(args: args!)
7272
}
73+
else if(call.method.elementsEqual(_methodTelegramShare)){
74+
let args = call.arguments as? Dictionary<String,Any>
75+
shareToTelegram(message: args!["msg"] as! String )
76+
}
7377
else{
7478
let args = call.arguments as? Dictionary<String,Any>
7579
systemShare(message: args!["msg"] as! String,result: result)
@@ -218,10 +222,27 @@ public class SwiftFlutterShareMePlugin: NSObject, FlutterPlugin, SharingDelegate
218222
}
219223
}
220224

221-
222-
223225
}
226+
//share via telegram
227+
//@ text that you want to share.
228+
func shareToTelegram(message: String,result: @escaping FlutterResult )
229+
{
230+
let telegram = "tg://msg?text=\(message)"
231+
var characterSet = CharacterSet.urlQueryAllowed
232+
characterSet.insert(charactersIn: "?&")
233+
let telegramURL = NSURL(string: telegram.addingPercentEncoding(withAllowedCharacters: characterSet)!)
234+
if UIApplication.shared.canOpenURL(telegramURL! as URL)
235+
{
236+
result("Sucess");
237+
UIApplication.shared.openURL(telegramURL! as URL)
238+
}
239+
else
240+
{
241+
result(FlutterError(code: "Not found", message: "WhatsAppBusiness is not found", details: "WhatsAppBusiness not intalled or Check url scheme."));
242+
}
224243

244+
}
245+
225246
//share via system native dialog
226247
//@ text that you want to share.
227248
func systemShare(message:String,result: @escaping FlutterResult) {

lib/flutter_share_me.dart

+15
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class FlutterShareMe {
1515
static const String _methodTwitter = 'twitter_share';
1616
static const String _methodInstagramShare = 'instagram_share';
1717
static const String _methodSystemShare = 'system_share';
18+
static const String _methodTelegramShare = 'telegram_share';
1819

1920
///share to WhatsApp
2021
/// [imagePath] is local image
@@ -60,6 +61,20 @@ class FlutterShareMe {
6061

6162
return result;
6263
}
64+
///share to Telegram
65+
/// [msg] message text you want on telegram
66+
Future<String?> shareToTelegram(
67+
{required String msg}) async {
68+
final Map<String, dynamic> arguments = <String, dynamic>{};
69+
arguments.putIfAbsent('msg', () => msg);
70+
String? result;
71+
try {
72+
result = await _channel.invokeMethod<String>(_methodTelegramShare, arguments);
73+
} catch (e) {
74+
return e.toString();
75+
}
76+
return result;
77+
}
6378

6479
///share to WhatsApp4Biz
6580
///[imagePath] is local image

pubspec.lock

+30-23
Original file line numberDiff line numberDiff line change
@@ -5,49 +5,49 @@ packages:
55
dependency: transitive
66
description:
77
name: async
8-
url: "https://pub.flutter-io.cn"
8+
url: "https://pub.dartlang.org"
99
source: hosted
10-
version: "2.6.1"
10+
version: "2.8.1"
1111
boolean_selector:
1212
dependency: transitive
1313
description:
1414
name: boolean_selector
15-
url: "https://pub.flutter-io.cn"
15+
url: "https://pub.dartlang.org"
1616
source: hosted
1717
version: "2.1.0"
1818
characters:
1919
dependency: transitive
2020
description:
2121
name: characters
22-
url: "https://pub.flutter-io.cn"
22+
url: "https://pub.dartlang.org"
2323
source: hosted
2424
version: "1.1.0"
2525
charcode:
2626
dependency: transitive
2727
description:
2828
name: charcode
29-
url: "https://pub.flutter-io.cn"
29+
url: "https://pub.dartlang.org"
3030
source: hosted
31-
version: "1.2.0"
31+
version: "1.3.1"
3232
clock:
3333
dependency: transitive
3434
description:
3535
name: clock
36-
url: "https://pub.flutter-io.cn"
36+
url: "https://pub.dartlang.org"
3737
source: hosted
3838
version: "1.1.0"
3939
collection:
4040
dependency: transitive
4141
description:
4242
name: collection
43-
url: "https://pub.flutter-io.cn"
43+
url: "https://pub.dartlang.org"
4444
source: hosted
4545
version: "1.15.0"
4646
fake_async:
4747
dependency: transitive
4848
description:
4949
name: fake_async
50-
url: "https://pub.flutter-io.cn"
50+
url: "https://pub.dartlang.org"
5151
source: hosted
5252
version: "1.2.0"
5353
flutter:
@@ -60,25 +60,32 @@ packages:
6060
description: flutter
6161
source: sdk
6262
version: "0.0.0"
63+
lint:
64+
dependency: "direct main"
65+
description:
66+
name: lint
67+
url: "https://pub.dartlang.org"
68+
source: hosted
69+
version: "1.7.2"
6370
matcher:
6471
dependency: transitive
6572
description:
6673
name: matcher
67-
url: "https://pub.flutter-io.cn"
74+
url: "https://pub.dartlang.org"
6875
source: hosted
6976
version: "0.12.10"
7077
meta:
7178
dependency: transitive
7279
description:
7380
name: meta
74-
url: "https://pub.flutter-io.cn"
81+
url: "https://pub.dartlang.org"
7582
source: hosted
76-
version: "1.3.0"
83+
version: "1.7.0"
7784
path:
7885
dependency: transitive
7986
description:
8087
name: path
81-
url: "https://pub.flutter-io.cn"
88+
url: "https://pub.dartlang.org"
8289
source: hosted
8390
version: "1.8.0"
8491
sky_engine:
@@ -90,58 +97,58 @@ packages:
9097
dependency: transitive
9198
description:
9299
name: source_span
93-
url: "https://pub.flutter-io.cn"
100+
url: "https://pub.dartlang.org"
94101
source: hosted
95102
version: "1.8.1"
96103
stack_trace:
97104
dependency: transitive
98105
description:
99106
name: stack_trace
100-
url: "https://pub.flutter-io.cn"
107+
url: "https://pub.dartlang.org"
101108
source: hosted
102109
version: "1.10.0"
103110
stream_channel:
104111
dependency: transitive
105112
description:
106113
name: stream_channel
107-
url: "https://pub.flutter-io.cn"
114+
url: "https://pub.dartlang.org"
108115
source: hosted
109116
version: "2.1.0"
110117
string_scanner:
111118
dependency: transitive
112119
description:
113120
name: string_scanner
114-
url: "https://pub.flutter-io.cn"
121+
url: "https://pub.dartlang.org"
115122
source: hosted
116123
version: "1.1.0"
117124
term_glyph:
118125
dependency: transitive
119126
description:
120127
name: term_glyph
121-
url: "https://pub.flutter-io.cn"
128+
url: "https://pub.dartlang.org"
122129
source: hosted
123130
version: "1.2.0"
124131
test_api:
125132
dependency: transitive
126133
description:
127134
name: test_api
128-
url: "https://pub.flutter-io.cn"
135+
url: "https://pub.dartlang.org"
129136
source: hosted
130-
version: "0.3.0"
137+
version: "0.4.2"
131138
typed_data:
132139
dependency: transitive
133140
description:
134141
name: typed_data
135-
url: "https://pub.flutter-io.cn"
142+
url: "https://pub.dartlang.org"
136143
source: hosted
137144
version: "1.3.0"
138145
vector_math:
139146
dependency: transitive
140147
description:
141148
name: vector_math
142-
url: "https://pub.flutter-io.cn"
149+
url: "https://pub.dartlang.org"
143150
source: hosted
144151
version: "2.1.0"
145152
sdks:
146-
dart: ">=2.12.0 <3.0.0"
153+
dart: ">=2.14.0-360.0.dev <3.0.0"
147154
flutter: ">=1.12.0"

0 commit comments

Comments
 (0)