Skip to content

Commit

Permalink
added Share To telegram
Browse files Browse the repository at this point in the history
  • Loading branch information
Nagacharan.K committed Sep 16, 2021
1 parent 7526cf9 commit f84502c
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 30 deletions.
13 changes: 12 additions & 1 deletion android/.settings/org.eclipse.buildship.core.prefs
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
connection.project.dir=../example/android
arguments=
auto.sync=false
build.scans.enabled=false
connection.gradle.distribution=GRADLE_DISTRIBUTION(WRAPPER)
connection.project.dir=
eclipse.preferences.version=1
gradle.user.home=
java.home=C\:/Program Files/Eclipse Foundation/jdk-11.0.12.7-hotspot
jvm.arguments=
offline.mode=false
override.workspace.settings=true
show.console.view=true
show.executions.view=true
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public class FlutterShareMePlugin implements MethodCallHandler, FlutterPlugin, A
final private static String _methodTwitter = "twitter_share";
final private static String _methodSystemShare = "system_share";
final private static String _methodInstagramShare = "instagram_share";
final private static String _methodTelegramShare = "telegram_share";


private Activity activity;
Expand Down Expand Up @@ -124,6 +125,10 @@ public void onMethodCall(MethodCall call, @NonNull Result result) {
msg = call.argument("url");
shareInstagramStory(msg, result);
break;
case _methodTelegramShare:
msg = call.argument("msg");
shareToTelegram(msg, result);
break;
default:
result.notImplemented();
break;
Expand Down Expand Up @@ -242,7 +247,30 @@ private void shareWhatsApp(String imagePath, String msg, Result result, boolean
result.error("error", var9.toString(), "");
}
}

/**
* share to telegram
*
* @param msg String
* @param result Result
*/

private void shareToTelegram(String msg, Result result) {
try {
String message = call.argument("msg");
Intent telegramIntent = new Intent(Intent.ACTION_SEND);
telegramIntent.setType("text/plain");
telegramIntent.setPackage("org.telegram.messenger");
telegramIntent.putExtra(Intent.EXTRA_TEXT, message);
try {
startActivity(telegramIntent);
result.success("true");
} catch (Exception ex) {
result.success("false");
}
} catch (Exception var9) {
result.error("error", var9.toString(), "");
}
}
/**
* share whatsapp message to personal number
*
Expand Down
11 changes: 11 additions & 0 deletions example/android/.settings/org.eclipse.buildship.core.prefs
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
arguments=
auto.sync=false
build.scans.enabled=false
connection.gradle.distribution=GRADLE_DISTRIBUTION(VERSION(7.0-rc-1))
connection.project.dir=
eclipse.preferences.version=1
gradle.user.home=
java.home=C\:/Program Files/Eclipse Foundation/jdk-11.0.12.7-hotspot
jvm.arguments=
offline.mode=false
override.workspace.settings=true
show.console.view=true
show.executions.view=true
13 changes: 11 additions & 2 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ enum Share {
whatsapp_personal,
whatsapp_business,
share_system,
share_instagram
share_instagram,
share_telegram
}

void main() => runApp(MyApp());
Expand Down Expand Up @@ -64,6 +65,10 @@ class _MyAppState extends State<MyApp> {
onPressed: () => onButtonTap(Share.share_instagram),
child: const Text('share to Instagram'),
),
ElevatedButton(
onPressed: () => onButtonTap(Share.share_telegram),
child: const Text('share to Telegram'),
),
ElevatedButton(
onPressed: () => onButtonTap(Share.share_system),
child: const Text('share to System'),
Expand Down Expand Up @@ -110,7 +115,8 @@ class _MyAppState extends State<MyApp> {
case Share.whatsapp:
if (file != null) {
response = await flutterShareMe.shareToWhatsApp(
imagePath: file!.path, fileType: videoEnable ? FileType.video : FileType.image);
imagePath: file!.path,
fileType: videoEnable ? FileType.video : FileType.image);
} else {
response = await flutterShareMe.shareToWhatsApp(msg: msg);
}
Expand All @@ -128,6 +134,9 @@ class _MyAppState extends State<MyApp> {
case Share.share_instagram:
response = await flutterShareMe.shareToInstagram(imagePath: file!.path);
break;
case Share.share_telegram:
response = await flutterShareMe.shareToTelegram(msg: msg);
break;
}
debugPrint(response);
}
Expand Down
27 changes: 24 additions & 3 deletions ios/Classes/SwiftFlutterShareMePlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class SwiftFlutterShareMePlugin: NSObject, FlutterPlugin, SharingDelegate
let _methodTwitter = "twitter_share";
let _methodInstagram = "instagram_share";
let _methodSystemShare = "system_share";
let _methodTelegramShare = "telegram_share";

var result: FlutterResult?
var documentInteractionController: UIDocumentInteractionController?
Expand Down Expand Up @@ -55,7 +56,6 @@ public class SwiftFlutterShareMePlugin: NSObject, FlutterPlugin, SharingDelegate
}
else if(call.method.elementsEqual(_methodWhatsAppPersonal)){
let args = call.arguments as? Dictionary<String,Any>

shareWhatsAppPersonal(message: args!["msg"]as! String, phoneNumber: args!["phoneNumber"]as! String, result: result)
}
else if(call.method.elementsEqual(_methodFaceBook)){
Expand All @@ -70,6 +70,10 @@ public class SwiftFlutterShareMePlugin: NSObject, FlutterPlugin, SharingDelegate
let args = call.arguments as? Dictionary<String,Any>
shareInstagram(args: args!)
}
else if(call.method.elementsEqual(_methodTelegramShare)){
let args = call.arguments as? Dictionary<String,Any>
shareToTelegram(message: args!["msg"] as! String )
}
else{
let args = call.arguments as? Dictionary<String,Any>
systemShare(message: args!["msg"] as! String,result: result)
Expand Down Expand Up @@ -218,10 +222,27 @@ public class SwiftFlutterShareMePlugin: NSObject, FlutterPlugin, SharingDelegate
}
}



}
//share via telegram
//@ text that you want to share.
func shareToTelegram(message: String,result: @escaping FlutterResult )
{
let telegram = "tg://msg?text=\(message)"
var characterSet = CharacterSet.urlQueryAllowed
characterSet.insert(charactersIn: "?&")
let telegramURL = NSURL(string: telegram.addingPercentEncoding(withAllowedCharacters: characterSet)!)
if UIApplication.shared.canOpenURL(telegramURL! as URL)
{
result("Sucess");
UIApplication.shared.openURL(telegramURL! as URL)
}
else
{
result(FlutterError(code: "Not found", message: "WhatsAppBusiness is not found", details: "WhatsAppBusiness not intalled or Check url scheme."));
}

}

//share via system native dialog
//@ text that you want to share.
func systemShare(message:String,result: @escaping FlutterResult) {
Expand Down
15 changes: 15 additions & 0 deletions lib/flutter_share_me.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class FlutterShareMe {
static const String _methodTwitter = 'twitter_share';
static const String _methodInstagramShare = 'instagram_share';
static const String _methodSystemShare = 'system_share';
static const String _methodTelegramShare = 'telegram_share';

///share to WhatsApp
/// [imagePath] is local image
Expand Down Expand Up @@ -60,6 +61,20 @@ class FlutterShareMe {

return result;
}
///share to Telegram
/// [msg] message text you want on telegram
Future<String?> shareToTelegram(
{required String msg}) async {
final Map<String, dynamic> arguments = <String, dynamic>{};
arguments.putIfAbsent('msg', () => msg);
String? result;
try {
result = await _channel.invokeMethod<String>(_methodTelegramShare, arguments);
} catch (e) {
return e.toString();
}
return result;
}

///share to WhatsApp4Biz
///[imagePath] is local image
Expand Down
53 changes: 30 additions & 23 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,49 @@ packages:
dependency: transitive
description:
name: async
url: "https://pub.flutter-io.cn"
url: "https://pub.dartlang.org"
source: hosted
version: "2.6.1"
version: "2.8.1"
boolean_selector:
dependency: transitive
description:
name: boolean_selector
url: "https://pub.flutter-io.cn"
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
characters:
dependency: transitive
description:
name: characters
url: "https://pub.flutter-io.cn"
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
charcode:
dependency: transitive
description:
name: charcode
url: "https://pub.flutter-io.cn"
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
version: "1.3.1"
clock:
dependency: transitive
description:
name: clock
url: "https://pub.flutter-io.cn"
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
collection:
dependency: transitive
description:
name: collection
url: "https://pub.flutter-io.cn"
url: "https://pub.dartlang.org"
source: hosted
version: "1.15.0"
fake_async:
dependency: transitive
description:
name: fake_async
url: "https://pub.flutter-io.cn"
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
flutter:
Expand All @@ -60,25 +60,32 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
lint:
dependency: "direct main"
description:
name: lint
url: "https://pub.dartlang.org"
source: hosted
version: "1.7.2"
matcher:
dependency: transitive
description:
name: matcher
url: "https://pub.flutter-io.cn"
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.10"
meta:
dependency: transitive
description:
name: meta
url: "https://pub.flutter-io.cn"
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.0"
version: "1.7.0"
path:
dependency: transitive
description:
name: path
url: "https://pub.flutter-io.cn"
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0"
sky_engine:
Expand All @@ -90,58 +97,58 @@ packages:
dependency: transitive
description:
name: source_span
url: "https://pub.flutter-io.cn"
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.1"
stack_trace:
dependency: transitive
description:
name: stack_trace
url: "https://pub.flutter-io.cn"
url: "https://pub.dartlang.org"
source: hosted
version: "1.10.0"
stream_channel:
dependency: transitive
description:
name: stream_channel
url: "https://pub.flutter-io.cn"
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
string_scanner:
dependency: transitive
description:
name: string_scanner
url: "https://pub.flutter-io.cn"
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
term_glyph:
dependency: transitive
description:
name: term_glyph
url: "https://pub.flutter-io.cn"
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
test_api:
dependency: transitive
description:
name: test_api
url: "https://pub.flutter-io.cn"
url: "https://pub.dartlang.org"
source: hosted
version: "0.3.0"
version: "0.4.2"
typed_data:
dependency: transitive
description:
name: typed_data
url: "https://pub.flutter-io.cn"
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.0"
vector_math:
dependency: transitive
description:
name: vector_math
url: "https://pub.flutter-io.cn"
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
sdks:
dart: ">=2.12.0 <3.0.0"
dart: ">=2.14.0-360.0.dev <3.0.0"
flutter: ">=1.12.0"

0 comments on commit f84502c

Please sign in to comment.